2016-10-31 4 views
0

私はコレクション内のドキュメントを見つけようとしています。私はほぼすべてのコレクションでこのクエリ構造を使用していますが、何らかの理由でこれはこのコレクションでは機能しません。どんな助け?MongoDB find()が必要なものを見つけることができません

今、このコレクション内のデータだけ [ { _id: 581757143389e565b5cf8124, companyProfileID: '86660a5b-7f61-4238-889d-1cc3087947b9', url: 'sentsoftware.com', appID: 1 } ]

クエリ構造:私は見つかりませ文書を取得していない保つ

function getCompany(companyUrl, app, callback) { 
 
\t MongoClient.connect(url, function(err, db) { 
 
\t \t if (err) { 
 
\t \t \t console.log(err); 
 
\t \t } else { 
 
\t \t \t console.log("We are connected"); 
 
\t \t } 
 
\t \t 
 
\t \t var collection = db.collection('Companies'); 
 
\t \t collection.find({url: companyUrl, appID: app}).toArray(function (err, result) { 
 
\t \t \t if (err) { 
 
\t \t \t \t console.log(err); 
 
\t \t \t \t callback(err); 
 
\t \t \t } else if (result.length) { 
 
\t \t \t \t console.log("found"); 
 
\t \t \t \t callback(result); 
 
\t \t \t } else { 
 
\t \t \t \t console.log("No document found"); 
 
\t \t \t \t callback(err); 
 
\t \t \t } 
 
\t \t }); 
 
\t }); 
 
}

。しかし、もし私が, appID: appの部分を取り出そうとすれば、それはその文書を見つけます。

+0

「アプリケーション」を「番号」として渡していますか?それがデータベースに格納されている方法です。 – robertklep

+1

おそらくそれは文字列と数字の問題です。 1または "1"を検索しています –

+0

はい数値として渡しています。私はそれを文字列に変更し、すぐにそれを見つけました。ありがとうございました!誰かが回答として投稿し、それを受け入れる。 –

答えて

0

Mongoクエリーは型固有のものです(数字はキャスト可能ですが)。数字を期待している文字列がある可能性があります。

あなたは数1

collection.find({appID: 1}) 
を求める場合は、文字列 "1" のために

collection.find({appID: "1"}) 

せればそれだけで、 "1"等しい文字列を使用して文書を返します。

(Double、Long、Int)の書類のみを返信します。

collection.find({ appID : { $type : "string" } }); // or type 2 for earlier mongo versions 
collection.find({ appID : { $type : "double" } }); // or type 1 for earlier mongo versions 
collection.find({ appID : { $type : "int" } }); // or type 16 for earlier mongo versions 

チェックアウトBSONタイプとドキュメントで$種類、現在(2016年11月11日):あなたは$型のクエリを持っているタイプ興味深いことにhttps://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type

それますキャストの間ダブルス、ロング、インツ;ので、これらを挿入:

db.collection("temp").insertOne({ "type" : "my double", "num" : new mongo.Double(1.0) }); 
db.collection("temp").insertOne({ "type" : "my long", "num" : new mongo.Long(1) }); 
db.collection("temp").insertOne({ "type" : "my int", "num" : 1 }); 

と探し:

{ "num" : 1 } 

は、3つの文書を返します。

関連する問題