如何在mongoDb中将数组添加到现有集合

时间:2021-12-14 21:18:06

I need to add an array of data to an existing collection in mongoDb.

我需要在mongoDb中为现有集合添加一组数据。

suppose my existing collection, say "planetSystem" is of type:

假设我现有的集合,比如说“planetSystem”的类型:

{
     "_id":"123123123",
     "key1":"value1",
     "key2":"value2" 
}

i need to add an array of the following type

我需要添加以下类型的数组

[
    {
        "amount":"678",
        "addedBy":"idontknow"
        "commission":"100"
    } ,
    {
        "amount":"800",
        "addedBy":"iwishiknew"
        "commission":"108"
    } 
]

so that the final collection looks like this

所以最终的集合看起来像这样

{
    "_id":"123123123",
    "key1":"value1",
    "key2":"value2",
    [
      {
        "amount":"678",
        "addedBy":"idontknow"
        "commission":"100"
      } ,
      {
        "amount":"800",
        "addedBy":"iwishiknew"
        "commission":"108"
      }
    ] 
}

i need help in the update query part of this. Thanks

我需要帮助更新查询部分。谢谢

1 个解决方案

#1


0  

You will have to add key to PlanetSystem collection,

您必须为PlanetSystem集合添加密钥,

var planetSystem= {
 "_id":"123123123",
 "key1":"value1",
 "key2":"value2" };

var arr = [{
    "amount":"678",
    "addedBy":"idontknow"
    "commission":"100"
} ,
{
    "amount":"800",
    "addedBy":"iwishiknew"
    "commission":"108"
}];

Add new key

添加新密钥

planetSystem.arr = arr;

Result,

结果,

var planetSystem={
"_id": "123123123",
"key1": "value1",
"key2": "value2",
"arr": [{
    "amount": "678",
    "addedBy": "idontknow",
    "commission": "100"
}, {
    "amount": "800",
    "addedBy": "iwishiknew",
    "commission": "108"
}]}

#1


0  

You will have to add key to PlanetSystem collection,

您必须为PlanetSystem集合添加密钥,

var planetSystem= {
 "_id":"123123123",
 "key1":"value1",
 "key2":"value2" };

var arr = [{
    "amount":"678",
    "addedBy":"idontknow"
    "commission":"100"
} ,
{
    "amount":"800",
    "addedBy":"iwishiknew"
    "commission":"108"
}];

Add new key

添加新密钥

planetSystem.arr = arr;

Result,

结果,

var planetSystem={
"_id": "123123123",
"key1": "value1",
"key2": "value2",
"arr": [{
    "amount": "678",
    "addedBy": "idontknow",
    "commission": "100"
}, {
    "amount": "800",
    "addedBy": "iwishiknew",
    "commission": "108"
}]}