简体   繁体   English

Mongo查询搜索更新一个$ elemmatch

[英]Mongo query search updateOne $elemmatch

I'm having trouble updating my database. 我在更新数据库时遇到问题。 I have a surveys collection and each document is as follows: 我有一个调查收集,每个文档如下:

{
    "_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
}

I'm calling this on my back-end using mongoose to try and update my database whenever a user responded to a survey. 每当用户响应调查时,我都会在其后端使用mongoose调用它来尝试更新数据库。

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

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

But the update is only successful when the query matches the first object in the recipients array. 但是,仅当查询与收件人数组中的第一个对象匹配时,更新才成功。 For example, this would work. 例如,这将起作用。 The query successfully updates the survey document and the recipient subdocument. 该查询成功更新了调查文档和收件人子文档。

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

But this doesn't work 但这行不通

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

These are my schemas 这些是我的模式

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 }
});

When I use node to try to query the database manually, it also only returns the survey when the query matches the first recipient subdocument. 当我使用节点尝试手动查询数据库时,它也仅在查询与第一个收件人子文档匹配时才返回调查。

This successfully finds the survey and returns it. 这将成功找到调查并返回调查。

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

These don't 这些不

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)

I have been trying to look up how $elemMatch works and someone told me that I can't query the properties of objects inside the recipients array, and that I can only query its IDs. 我一直在尝试查看$ elemMatch的工作方式,有人告诉我,我无法查询收件人数组中对象的属性,而只能查询其ID。

问题是您在存储后续电子邮件时没有对它们进行修剪,因为您会看到只有第一封电子邮件已被正确修剪,而其余电子邮件的开头都有空白,使查询不返回任何内容。

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

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