Mongodb 如何将项推入数组中对象的数组中

Mongodb 如何将项推入数组中对象的数组中,mongodb,mongodb-query,Mongodb,Mongodb Query,我在MongoDB中有以下对象 我想对它们进行更新,这样对于每个文档,如果daysfree包含一个字段date设置为“2020-10-12T21:00:00.00Z”的对象,那么在同一个对象中推送到friends一个新字符串 以下是我尝试过的问题,它们完成了一半的工作。 问题是查询只更新第一个文档,而不是两个文档 注意。更新仅更新第一个匹配的文档。可以使用updateMany(我更喜欢),也可以放置可选参数{multi:true} db.collection.update({ &

我在MongoDB中有以下对象

我想对它们进行更新,这样对于每个文档,如果
daysfree
包含一个字段
date
设置为
“2020-10-12T21:00:00.00Z”
的对象,那么在同一个对象中推送到
friends
一个新字符串

以下是我尝试过的问题,它们完成了一半的工作。

问题是查询只更新第一个文档,而不是两个文档


注意。

更新
仅更新第一个匹配的文档。可以使用
updateMany
(我更喜欢),也可以放置可选参数
{multi:true}

db.collection.update({
    "daysfree":{
        $elemMatch:{
            "date":{
                "$gte":ISODate("2020-10-12T00:00:00Z"),
                "$lt": ISODate("2020-10-13T00:00:00Z")
                }
            }
        }
    },
    { $push:{ "daysfree.$.friends":"Josef" } },
    { multi: true }
);

db.collection.updateMany({
    "daysfree":{
        $elemMatch:{
            "date":{
                "$gte":ISODate("2020-10-12T00:00:00Z"),
                "$lt": ISODate("2020-10-13T00:00:00Z")
                }
            }
        }
    },
    { $push:{ "daysfree.$.friends":"Josef" } },
);
db.collection.update({
    "daysfree":{
        $elemMatch:{
            "date":{
                "$gte":ISODate("2020-10-12T00:00:00Z"),
                "$lt": ISODate("2020-10-13T00:00:00Z")
                }
            }
        }
    },{
        $push:{
            "daysfree.$.friends":"Josef"
        }
    }
);
db.collection.update({
    "daysfree":{
        $elemMatch:{
            "date":{
                "$gte":ISODate("2020-10-12T00:00:00Z"),
                "$lt": ISODate("2020-10-13T00:00:00Z")
                }
            }
        }
    },
    { $push:{ "daysfree.$.friends":"Josef" } },
    { multi: true }
);

db.collection.updateMany({
    "daysfree":{
        $elemMatch:{
            "date":{
                "$gte":ISODate("2020-10-12T00:00:00Z"),
                "$lt": ISODate("2020-10-13T00:00:00Z")
                }
            }
        }
    },
    { $push:{ "daysfree.$.friends":"Josef" } },
);