猫鼬update或updateMany到子文档返回了相同的ObjectId

时间:2019-05-16 07:20:26

标签: mongodb mongoose

我只想知道。子文档上出现的update或updateMany结果是否会产生相同的ObjectId?

我的查询是否存在错误,或者这是mongoose / mongodb中的错误?

我的猫鼬查询使用update或updateMany推送数据

使用更新:

const announcement = await Team.update({}, {
    $push: {
       announcements: {desc}
    }}, {multi: true, runValidators: true});

if(announcement.nModified === 0) throw new Error('');

res.send(announcement);
next();

使用updateMany:

const announcement = await Team.updateMany({}, {
    $push: {
       announcements: {desc}
    }}, {runValidators: true});

if(announcement.nModified === 0) throw new Error('');

res.send(announcement);
next();

这是mongodb文档中的结果:

team: [
    {
        "name": "team 1"
        "announcements": [
            {
                "_id": "5cdd0bd9cc45120426c89f1c",
                "desc" : "test"
            }
        ]
    },
    {
        "name": "team 2"
        "announcements": [
            {
                "_id": "5cdd0bd9cc45120426c89f1c",
                "desc" : "test"
            }
        ]
    },
    {
        "name": "team 3"
        "announcements": [
            {
                "_id": "5cdd0bd9cc45120426c89f1c",
                "desc" : "test"
            }
        ]
    }
]

如您所见。当我运行查询时,它将生成新数据,并将其推送到“公告”子文档中。但是奇怪的是,它们的ObjectId都是一样的。

真的是这样吗?还是一个错误?还是我的查询错了?

0 个答案:

没有答案