简体   繁体   English

如何在MongoDB中更新数组?

[英]How to update array in MongoDB?

I have about 40 documents in my collection. 我的收藏中大约有40份文件。 I need to update some elements in two arrays. 我需要更新两个数组中的某些元素。 The arrays of the document looks like 文档的数组看起来像

 "array1": [
    {
        "id" : "2030170",
        "date1" : ISODate("2016-01-04T07:10:00.000Z"),
        "date2" : ISODate("2016-01-04T08:00:00.000Z")
    }
 ],
 "array2": [
    {
        "Id" : "43463565",
        "date1" : ISODate("2016-01-04T07:10:00.000Z"),
        "date2" : ISODate("2016-01-04T08:00:00.000Z")
    }
  ]

How can I update fields date1 and date 2 in both arrays? 如何更新两个数组中的date1和date 2字段?

I am using java script for updating another data also. 我也在使用Java脚本更新其他数据。 It looks like 看起来像

db.getCollection('my.collec').update({ "myId" : { $gte: 293, $lte: 438}},
                                          {

                                          "$currentDate": { "time1": true },
                                          "$set": {
                                          "time2": true,
                                          "time3": true

                                          }
                                            },   { multi: true }
                                          )

I was trying this way: 我正在尝试这种方式:

  db.getCollection('my.collec').update({ "myId" : { $gte: 293, $lte: 438}},
                                              {

                                              "$currentDate": { "time1": true },
                                              "$set": {
                                              "time2": true,
                                              "time3": true,

                              "array1.0.date1": true,
                              "array1.0.date2": true,
                              "array2.0.date1": true,
                              "array2.0.date2": true
                                              }
                                                },   { multi: true }
                                              )

But instead of currentDate I get "true". 但是,我得到的不是“ currentDate”,而是“ true”。 With variables like time everything works fine. 使用像时间这样的变量,一切正常。

I'm wondering why all the documents aren't in one array? 我想知道为什么所有文档都没有放在一个数组中? In any event try: 无论如何,请尝试:

db.YOURCOLLECTION.update(

{$and: [ 
    { date1: {$lt: $currentDate}}, 
    { date2: {$lt: $currentDate}}
    ] 
},

{
date1 : $currentDate,
date2 : $currentDate
},

{upsert: true}
)

Edit: one thing I noticed $currentDate I don't think converts to ISO automatically so you would need to either explicitly input the desired time stamp (per the gentleman above) replacing $currentDate with that value in ISO in the update expression above or use a conversion library. 编辑:我注意到$ currentDate的一件事,我认为$ currentDate不会自动转换为ISO,因此您需要在上面的更新表达式中显式输入所需的时间戳记(根据上述绅士的说法),用$ ISO的值替换$ currentDate或使用转换库。

See: https://docs.mongodb.org/manual/reference/operator/update/currentDate/ 参见: https : //docs.mongodb.org/manual/reference/operator/update/currentDate/

I need to update some elements in some arrays 我需要更新某些数组中的某些元素

Would be nice to know what the criteria is 知道标准是什么会很高兴

How can I update fields date1 and date 2 in both arrays such that these fields will not be added to objects that do not have them? 如何更新两个数组中的date1和date 2字段,以使这些字段不会添加到没有它们的对象中?

Assuming that you are only updating array1 and array2 first element 假设您仅更新array1和array2第一个元素

db.josef.update({
    "array1": {
        $size: 1
    },
    "array2": {
        $size: 1
    }
},
{
    $set: {
        "array1.0.date1": ISODate("2011-01-01T00:00:00.0000Z"),
        "array1.0.date2": ISODate("2011-01-01T00:00:00.0000Z"),
        "array2.0.date1": ISODate("2011-01-01T00:00:00.0000Z"),
        "array2.0.date2": ISODate("2011-01-01T00:00:00.0000Z")
    }
},
{
    multi: true
})

How can I update fields date1 and date 2 in both arrays such that these fields will not be added to objects that do not have them? 如何更新两个数组中的date1和date 2字段,以使这些字段不会添加到没有它们的对象中?

upsert is false by default so it won't be added. 默认情况下,upsert为false,因此不会添加。

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

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