简体   繁体   English

在文档MongoDB中添加到列表

[英]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. 这些项目具有和ID,以及子ID。 The way I am currently storing everything is: 我目前存储所有内容的方式是:

Each ID has its own collection 每个ID都有自己的集合

Within the collection there is a document for each subID. 在集合中,每个子ID都有一个文档。 The document for each subID is layed out like so: 每个subID的文档布局如下:

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

I need to be able to add to the list of playerNames quickly and efficiently. 我需要能够快速有效地添加到playerNames列表中。

Thank you! 谢谢!

If I understood your question correctly, you need to add items to the "playerNames" array. 如果我正确理解了您的问题,则需要将项目添加到“ playerNames”数组中。 You can use one of the following operators: 您可以使用以下运算符之一:

  1. If the player names array will have unique elements in it, use $addToSet 如果玩家名称数组中将包含唯一元素,请使用$ addToSet
  2. Otherwise, use $push 否则,请使用$ push

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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