Mongo查询搜索updateOne $ elemmatch

时间:2018-03-20 09:12:09

标签: javascript mongodb mongoose

我无法更新数据库。我有一个调查集,每个文件如下:

{
    "_id": {
        "$oid": "5aaf4f7984521736d88db4bb"
    },
    "title": "4242 ",
    "body": "4242 ",
    "subject": "4242 ",
    "recipients": [
        {
            "email": "a@gmail.com",
            "responded": false,
            "_id": {
                "$oid": "5ab04084be3c1529bcbdcd6e"
            }
        },
        {
            "email": " b@gmail.com",
            "responded": false,
            "_id": {
                "$oid": "5ab04084be3c1529bcbdcd6d"
            }
        },
        {
            "email": " c@gmail.com",
            "responded": false,
            "_id": {
                "$oid": "5ab04084be3c1529bcbdcd6c"
            }
        },
        {
            "email": " d@gmail.com",
            "responded": false,
            "_id": {
                "$oid": "5ab04084be3c1529bcbdcd6b"
            }
        },
        {
            "email": " e@gmail.com",
            "responded": false,
            "_id": {
                "$oid": "5ab04084be3c1529bcbdcd6a"
            }
        }
    ],
    "_user": {
        "$oid": "5aa5edbf8887a21af8a8db4c"
    },
    "dateSent": {
        "$date": "2018-03-20T00:11:55.943Z"
    },
    "yes": 0,
    "no": 0,
    "__v": 0
}

我在后端使用mongoose来调用它来尝试在用户响应调查时更新我的​​数据库。

const Survey = mongoose.model('surveys');

Survey.updateOne(
    {
        _id: surveyId,
        recipients: {
            $elemMatch: { email: email, responded: false }
        }
    }, 
    {
        $inc: { [choice]: 1 },
        $set: { 'recipients.$.responded': true }
    }
).exec();

但只有当查询与收件人数组中的第一个对象匹配时,更新才会成功。例如,这可行。该查询成功更新调查文档和收件人子文档。

Survey.updateOne(
    {
        _id: surveyId,
        recipients: {
            $elemMatch: { email: "a@gmail.com", responded: false }
        }
    }, 
    {
        $inc: { [choice]: 1 },
        $set: { 'recipients.$.responded': true }
    }
).exec();

但这不起作用

Survey.updateOne(
    {
        _id: surveyId,
        recipients: {
            $elemMatch: { email: "b@gmail.com", responded: false }
        }
    }, 
    {
        $inc: { [choice]: 1 },
        $set: { 'recipients.$.responded': true }
    }
).exec();

这些是我的架构

const surveySchema = new Schema({
    title: String,
    body: String,
    subject: String,
    recipients: [RecipientSchema],
    yes: { type: Number, default: 0 },
    no: { type: Number, default: 0 },
    _user: { type: Schema.Types.ObjectId, ref: 'User' },
    dateSent: Date,
    lastResponded: Date
});

const recipientSchema = new Schema({
    email: String,
    responded: { type: Boolean, default: false }
});

当我使用node尝试手动查询数据库时,它也只会在查询与第一个收件人子文档匹配时返回调查。

这成功找到了调查并将其返回。

Survey.find(
    {   
        _id: "5aaf4f7984521736d88db4bb", 
        recipients: { 
            $elemMatch: {
                email: "a@gmail.com",
                responded: false
            }
    }
}).then(console.log)

这些不是

Survey.find(
    {   
        _id: "5aaf4f7984521736d88db4bb", 
        recipients: { 
            $elemMatch: {
                email: "b@gmail.com",
                responded: false
            }
    }
}).then(console.log)

Survey.find(
    {   
        _id: "5aaf4f7984521736d88db4bb", 
        recipients: { 
            $elemMatch: {
                email: "c@gmail.com",
                responded: false
            }
    }
}).then(console.log)

我一直试图查看$ elemMatch是如何工作的,有人告诉我,我无法查询收件人数组中对象的属性,并且我只能查询其ID。

1 个答案:

答案 0 :(得分:0)

问题是您在不修剪后续存储后续电子邮件的情况下,因为您只能看到第一封电子邮件被正确修剪,但其余部分在开头都有空格,使查询不返回任何内容。 / p>