在多个字段上设置相同的值

时间:2022-12-27 11:48:18

I have the following document structure:

我有以下文档结构:

{
    ...
    timings: {
        mon: "",
        tue: "",
        wed: "",
        thu: "",
        fri: "",
        sat: "",
        sun: "",
    }
    ...
}

I want to set the value "Open 24 Hours" for all the days of the week.

我想为一周中的所有日子设置“24小时开放”值。

Currently I am using the query:

目前我正在使用查询:

db.collection.update({_id: ObjectId("someId")}, {$set: {"timings.mon": "Open 24 Hours"}});

And then running the same for the rest of the days of the week.

然后在一周的其余时间运行相同的操作。

I can also set all the fields in the same query explicitly, but it's faster to just change the day and run it again.

我也可以明确地在同一个查询中设置所有字段,但只需更改日期并再次运行它就会更快。

Is there a way to set the value of multiple fields in a subdocument in a more efficient way?

有没有办法以更有效的方式设置子文档中的多个字段的值?

UPDATE

I tried the following query, but it doesn't work:

我尝试了以下查询,但它不起作用:

db.collection.update({_id: ObjectId("someId")}, {$set: {"timings.$": "Open 24 Hours"}});

1 个解决方案

#1


You could try first creating the update object where you can set the multiple fields of that object before updating the document this way:

您可以尝试首先创建更新对象,您可以在以这种方式更新文档之前设置该对象的多个字段:

var days = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
    timings = {},
    query = {"_id" : ObjectId("554e12e4674dec7517e692eb")},
    update = {
        "$set": { "timings": timings }
    },
    options = {"upsert": true};

days.forEach(function (day){ timings[day] = "Open 24 Hours" });
db.collection.update(query, update, options);

Querying the collection for that document using db.collection.findOne(query) will yield:

使用db.collection.findOne(query)查询该文档的集合将产生:

/* 0 */
{
    "_id" : ObjectId("554e12e4674dec7517e692eb"),
    "timings" : {
        "mon" : "Open 24 Hours",
        "tue" : "Open 24 Hours",
        "wed" : "Open 24 Hours",
        "thu" : "Open 24 Hours",
        "fri" : "Open 24 Hours",
        "sat" : "Open 24 Hours",
        "sun" : "Open 24 Hours"
    }
}

-- UPDATE --

- 更新 -

Another approach (as suggested by @Michael in the comments) is to use the JavaScript's native Object.keys() method which returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well):

另一种方法(由@Michael在评论中提出)是使用JavaScript的本机Object.keys()方法,该方法返回给定对象自己的可枚举属性的数组,其顺序与for ... in提供的顺序相同。循环(不同之处在于for-in循环也枚举了原型链中的属性):

> var days = Object.keys(db.collection.findOne().timings);
> print(days);
mon,tue,wed,thu,fri,sat,sun
>

And thus can be implemented as:

因此可以实现为:

var days = Object.keys(db.collection.findOne().timings),
    timings = {},
    query = {"_id" : ObjectId("554e12e4674dec7517e692eb")},
    update = {
        "$set": { "timings": timings }
    },
    options = {"upsert": true};

#1


You could try first creating the update object where you can set the multiple fields of that object before updating the document this way:

您可以尝试首先创建更新对象,您可以在以这种方式更新文档之前设置该对象的多个字段:

var days = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
    timings = {},
    query = {"_id" : ObjectId("554e12e4674dec7517e692eb")},
    update = {
        "$set": { "timings": timings }
    },
    options = {"upsert": true};

days.forEach(function (day){ timings[day] = "Open 24 Hours" });
db.collection.update(query, update, options);

Querying the collection for that document using db.collection.findOne(query) will yield:

使用db.collection.findOne(query)查询该文档的集合将产生:

/* 0 */
{
    "_id" : ObjectId("554e12e4674dec7517e692eb"),
    "timings" : {
        "mon" : "Open 24 Hours",
        "tue" : "Open 24 Hours",
        "wed" : "Open 24 Hours",
        "thu" : "Open 24 Hours",
        "fri" : "Open 24 Hours",
        "sat" : "Open 24 Hours",
        "sun" : "Open 24 Hours"
    }
}

-- UPDATE --

- 更新 -

Another approach (as suggested by @Michael in the comments) is to use the JavaScript's native Object.keys() method which returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well):

另一种方法(由@Michael在评论中提出)是使用JavaScript的本机Object.keys()方法,该方法返回给定对象自己的可枚举属性的数组,其顺序与for ... in提供的顺序相同。循环(不同之处在于for-in循环也枚举了原型链中的属性):

> var days = Object.keys(db.collection.findOne().timings);
> print(days);
mon,tue,wed,thu,fri,sat,sun
>

And thus can be implemented as:

因此可以实现为:

var days = Object.keys(db.collection.findOne().timings),
    timings = {},
    query = {"_id" : ObjectId("554e12e4674dec7517e692eb")},
    update = {
        "$set": { "timings": timings }
    },
    options = {"upsert": true};