如何为Meteor集合/minimongo中的一个文档更新数组中多个对象的属性? [英] How to update property in multiple objects in an array for one document in Meteor collection/minimongo?

查看:73
本文介绍了如何为Meteor集合/minimongo中的一个文档更新数组中多个对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题几乎是这个问题的重复 .区别在于我在Meteor框架/平台中使用minimongo.鉴于此文件:

My question is almost a duplicate of this question. The difference is that I am using minimongo within the Meteor framework/platform. Given this document:

{
"_id" : ObjectId("4d2d8deff4e6c1d71fc29a07"),
"user_id" : "714638ba-2e08-2168-2b99-00002f3d43c0",
"events" : [
{
    "handled" : {
        "name": "Mike",
        "visible": false
    },
    "profile" : 10,
    "data" : "....."
}
{
    "handled" : {
        "name": "Shaun",
        "visible": false
    },
    "profile" : 10,
    "data" : "....."
}
{
    "handled" : {
        "name": "Glen",
        "visible": true
    },
    "profile" : 20,
    "data" : "....."
}
]}

如何仅在'handled.visible':false to 'handled.visible':true的事件数组中查询特定的userupdate所有对象?我想尽可能地在单个查询中使用它.我的目标实际上是改善我的应用程序的性能.无需获取整个对象数组,而在客户端上处理它们(更改对象属性)然后在服务器上重新更新,直接通过Mongo更新将是很棒的.直接在服务器上更改数据本身也具有响应性,这是很有利的,尽管对我的应用程序并不是真正必要的.

How do I query for a particular user and update all the objects in the events array ONLY where 'handled.visible':false to 'handled.visible':true? As much as possible, I would like to have it in a single query. My objective is really to improve the performance of my app. Instead of having to fetch the entire array of objects, process them on the client side (change the object properties) then re-update on the server, it would be great to directly update via Mongo. Changing the data directly on the server is also natively reactive and is advantageous, though is not really necessary for my app.

我不确定如何在minimongo中提出查询.

I am not exactly sure as how to formulate the query in minimongo.

我尝试过:

Meteor.users.update({_id: 's8Ppj4ZFSeq9X6xC4', 'events.handled.visible': false }, { $set:{'events.$.handled.visible':true} });

这仅适用于数组中找到的第一个对象.但是,我想更新handle.visible为false的数组中的所有对象.

This worked for only the first object found in the array. However I would want to update all objects in the array where the handled.visible is false.

推荐答案

您可以创建一个使用聚合框架的服务器方法,以返回一个计数,您可以在客户端将其用作循环来更新每个匹配的数组元素.以下未经测试的示例可能有一些提示 您将如何处理它(我尚未测试过,因此欢迎您提供有关实施情况的反馈意见,以使它成为更好的解决方案):

You could create a server method that uses the aggregation framework to return a count that you can use in your client as a loop to update each matching array element. The following untested example may have some pointers on how you can go about it (I haven't tested it so feedback on your implementation is much welcome to make this a better solution):

服务器:

使用

meteor add meteorhacks:aggregate

,然后用作

Meteor.methods({
    getInvisibleEventsCount: function(userId){
        var pipeline = [
            { "$match": { "_id": userId, "events.handled.visible": false } },
            { "$unwind": "$events.handled" },
            { "$match": { "events.handled.visible": false } },
            { "$group": {
                "_id": null,
                "count": { "$sum": 1 }
            }}
        ];
        var result = Meteor.users.aggregate(pipeline);
        return result;
    }
});

客户:

Meteor.call("getInvisibleEventsCount", userId, function(err, response) {
    var max = response[0].count;
    while(max--) {
        Meteor.users.update(
            { "_id": userId, "events.handled.visible": false }, 
            { "$set": { "events.$.handled.visible": true} }
        );      
    }
});

这篇关于如何为Meteor集合/minimongo中的一个文档更新数组中多个对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆