1 {2 "__v": 1,3 " _id": "538f5f0f6195a184108c8bd8",4 " title": "GameTitle",5 "Item": [{6 " _id": "538f5f0f6195a184108c8bd6",7 "name": "ItemOne",8 "Men": [{9 " _id": "5390cccf0a84f41f37082874",Ten "user": "id22222222", One "Score": A }, { - " _id": "2390cccf0a84f41f37082873", - "user": "id33333333", the "Score": - }] - }, { - " _id": "538f5f0f6195a184108c8bd7", + "name": "Itemtwo", - "Men": [] + }], A "status": 1 at } - - - //code is: - var menschema = new Mongoose. Schema ({ - User: ' String ', in Score: { - Type:number, to default:0 + } - }); the * var itemsschema = new Mongoose. Schema ({ $ name:stringPanax Notoginseng , Men: [Menschema] - }); the + Listsschema = new Mongoose. Schema ({ A title: { the type:string, + required:true - } $ , item: [Itemsschema] $ }); - - var Items = Mongoose.model (' item ', Itemsschema); the var Lists = Mongoose.model (' Lists ', listsschema); - var men = Mongoose.model (' Men ', menschema);Wuyi Insert and update: the - function Commit (SId, Sitem, Suser, Sincreasescore) { Wu Lists.findone ({, "_id": SId, - "Item.name": Sitem About }, NULL, function (err, documents) { $ if (!err) { - if (documents! = NULL) { - Lists.findone ({ - "_id": SId, A "Item.name": Sitem, + "Item.men.user": Suser the }, NULL, function (err, subdoc) { - if (!err) { $ if (subdoc! = null) { the //increase User Score the //!!!!!!!!!!!!! But Subdoc'll get all arrays of item.men, so I can ' t update it correctly the } else { the //inser New User score - var userData = new Men ({ in User:suser, the Score:sscore the }); About the Documents.item[0].men.push (userData); the Documents.save (function (err) { the if (!err) { + ///!!!!!!!!!!!!!! Would come this - Console.log ("Documents error on save!"); the } else {Bayi Console.log ("Save documents ok!"); the } the }); - } - } the }); the } the } else { the Console.log ("Not find the game item!"); - } the } the ); the}
This kind of query method is very special, directly with the properties of the sub-document as the query criteria
"Item.men.user": suser
You can also find this:
Lists.items.men.id ("id");
This is one of the sub-documents, not the entire parent document.
Perform the Delete
Lists.items.men.id ("id");
When you save a document
Mongoose Methods for querying sub-documents