Mongoose Methods for querying sub-documents

Source: Internet
Author: User
Tags findone

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.