简体   繁体   中英

Add to list within document MongoDB

I have a database where I store player names that belong to people who have certain items. The items have and IDs, and subIDs. The way I am currently storing everything is:

Each ID has its own collection

Within the collection there is a document for each subID. The document for each subID is layed out like so:

{ "itemID":itemID, "playerNames":[playerName1, playerName2, playerName3] }

I need to be able to add to the list of playerNames quickly and efficiently.

Thank you!

If I understood your question correctly, you need to add items to the "playerNames" array. You can use one of the following operators:

  1. If the player names array will have unique elements in it, use $addToSet
  2. Otherwise, use $push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM