aggregation-framework

Reverse populate in mongoose

孤者浪人 提交于 2020-08-27 21:27:04
问题 How can i reverse populate in mongo. I have 2 schema's User: var user_scheme = new mongoose.Schema({ name:String, age:Number, roles:{ type:mongoose.Schema.Types.ObjectId, ref:'Role' } }); Role: var role_scheme = new mongoose.Schema({ name:String, }); Documents: ///user { "_id" : ObjectId("5bf9a19b01ce3e19b440aed8"), "name" : "user1", "age" : 22, "roles" : ObjectId("5c0242621ab7b677e6b2e01e"), "__v" : 0 } ///role { "_id" : ObjectId("5c0242621ab7b677e6b2e01e"), "name" : "Admin" } Code: User

$lookup when foreignField is in nested array

假装没事ソ 提交于 2020-08-27 06:43:08
问题 I have two collections : Student { _id: ObjectId("657..."), name:'abc' }, { _id: ObjectId("593..."), name:'xyz' } Library { _id: ObjectId("987..."), book_name:'book1', issued_to: [ { student: ObjectId("657...") }, { student: ObjectId("658...") } ] }, { _id: ObjectId("898..."), book_name:'book2', issued_to: [ { student: ObjectId("593...") }, { student: ObjectId("594...") } ] } I want to make a Join to Student collection that exists in issued_to array of object field in Library collection. I

MongoDB — Find duplicate documents by multiple keys

∥☆過路亽.° 提交于 2020-08-25 04:48:11
问题 I have a collection with documents that look like the following: { "_id" : ObjectId("55b377cb66b393427367c3e2"), "comment" : "This is a comment", "url_key" : "55b377cb66b393427367c3df", //This is an ObjectId from another record in a different collection } I need to find records in this collection that contain duplicate values for the both the comment AND the url_key. I can easily generate (using aggregate) duplicate records for the same, single, key (eg: comment), but I can't figure out how