jeudi 2 avril 2015

MongoDb.js / Mongoose find() translate a String in ObjectId

I have two schemas utilizing Mongoose


Schema 1 var schema = mongoose.Schema({



name: {
type: String,
required: true
}
});
return mongoose.model('User', schema);


Schema 2


var schema = mongoose.Schema({



name: {
type: String,
required: true
},
user: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true
}
});
return mongoose.model('Page', schema);


My Data in Page Collection


_id

551b0cdf63dc96e9c39de0f8

551b0d1563dc96e9c39de0f9

551b0d2d63dc96e9c39de0fa

551b0d4363dc96e9c39de0fb

551daae0f4cb312c62dcbc1e


name

Cazaquistao

Russia

China

Australia

El Salvador


user

5515c7aaaf6d59fea26d7185

5515c7aaaf6d59fea26d7185

5515c7aaaf6d59fea26d7185

5515c7aaaf6d59fea26d7185

5515c7c9af6d59fea26d7186


when i search in schema 2 by user, I dont find any results.


The mongoose translate the mongoose converts the query erroneously.


Query



var test = Page.find().select("_id").where({"user" : "5515c7aaaf6d59fea26d7185"}).exec()
.then(function (t) {
console.log("hi", t);
});


Query Translated erroneously

db.pages.find({ user: ObjectId("5515c7aaaf6d59fea26d7185") })


no results


Query as it should be

db.pages.find({ user: "5515c7aaaf6d59fea26d7185" })

4 results


Any suggestions of what to do to work around this?


Aucun commentaire:

Enregistrer un commentaire