2016-10-31 2 views
0

Ich versuche ein Dokument in einer Sammlung zu finden. Ich habe diese Abfragestruktur in fast allen meinen Sammlungen verwendet, aber aus irgendeinem Grund funktioniert das nicht für diese Sammlung. Irgendeine Hilfe?MongoDB find() findet nicht, was ich brauche

Die einzigen Daten, die in dieser Sammlung jetzt [ { _id: 581757143389e565b5cf8124, companyProfileID: '86660a5b-7f61-4238-889d-1cc3087947b9', url: 'sentsoftware.com', appID: 1 } ]

Query-Struktur:

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 }); 
 
}

Ich halte kein Dokument gefunden zu bekommen. Aber wenn ich den , appID: app Teil herausnehmen würde, findet es das Dokument.

+0

Übergeben Sie 'App' als' Nummer'? So ist es in der Datenbank gespeichert. – robertklep

+1

Vielleicht ist es ein String-gegen-Nummer-Problem. Sind Sie auf der Suche nach 1 oder "1" –

+0

Ja, ich gebe es als eine Nummer. Ich änderte es zu einer Schnur und es fand es sofort. Vielen Dank! Jemand Post als Antwort und Ill akzeptiere es –

Antwort

0

Eine Mongo-Abfrage ist typenspezifisch (obwohl Zahlen umgewandelt werden können), Sie haben wahrscheinlich eine Zeichenfolge, in der Sie eine Zahl erwarten.

Wenn Sie die Zeichenfolge "1" stellen,

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

es wird nur Dokumente zurück mit einem String gleich "1", wenn Sie fragen, für die Nummer 1

collection.find({appID: 1}) 

es wird nur zurückgeben Dokumente mit einer echten Zahl (Doppel, Lang, Int) in.

Sie können prüfen, welche Arten Sie haben mit einem $ Typ query:

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 

Sehen Sie sich die BSON Typen und $ Art in der Dokumentation, die derzeit (2016.11.11): https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type

es Interessanter wird Besetzung zwischen Doppel, Longs und Ints; so Einsetzen diese:

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 }); 

und die Suche nach:

{ "num" : 1 } 

die drei Dokumente zurückgibt.

Verwandte Themen