子文档切换布尔值 [英] Toggle boolean value of subdocuments

查看:127
本文介绍了子文档切换布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图切换数组是对象的集合里面的布尔值,问题是那场被触发两个内线阵列两个对象,我想切换它只是一个对象。

i am trying to toggle the boolean value inside an array which is a collection of objects, problem is that field is being triggered for two both objects inside array, and i want to toggle it for a one object only.

文件:

"Invitation" : [ 
    {
        "__v" : 0,
        "ID" : ObjectId("54afaabd88694dc019d3b628"),
        "__t" : "USER",
        "_id" : ObjectId("54b5022b583973580c706784"),
        "Accepted" : false
    }, 
    {
        "__v" : 0,
        "ID" : ObjectId("54af6ce091324fd00f97a15f"),
        "__t" : "USER",
        "_id" : ObjectId("54bde39cdd55dd9016271f14"),
        "Accepted" : false
    }
]

控制器:

User.find({_id: req.user._id},'Invitation',function(err,docs) {
    if (err) {
        console.log(err);
    }
        var results = [];

        async.each(docs,function(doc,callback) {

            async.each(doc.Invitation,function(invite,callback) {
                User.findOneAndUpdate(
                    {'_id': doc._id, 'Invitation._id': invite._id},
                    {'$set': {'Invitation.$.Accepted': !invite.Accepted}},
                    function(err,doc) {
                        results.push(doc);
                        callback(err);
                    }
                );
            },callback);
        },function(err) {
            if (err)
                console.log(err);

            console.log('end'+results);
        });

});

更多:

邀请数组中ID字段是人OBJECTID,让我们说的鲍勃大卫发送邀请函,所以有邀请阵列内的两个对象,这意味着我有两个邀请从两个不同的人,即(鲍伯和大卫)现在我想接受鲍勃的邀请而已,所以当我接受鲍勃的邀请,接受字段应该被触发为真正的数据库鲍勃对象的,现在这在下面的答案显示的结果两个对象设置为真正,在这里我只希望接受邀请,是真实的。

ID field inside Invitation array is objectId of person, let's say Bob and David send Invitations, so there are two objects inside Invitation array, that means i have two Invitations from two different person, i.e ( Bob and David) now i want to accept invitation of Bob only, so when i accept invitation of Bob, Accepted field should be triggered as true of Bob object in database, now the results that are shown in below answer have both objects set to true, where i only want accepted invitation to be true.

同样是我发生的事情时,我只能接受一个用户/人两个对象获得真正的邀请。

Same is happening with me when i accept invitation of only one user/person both objects are getting true.

推荐答案

我看不到你要求的结果,这意味着它为我的作品和世界其他地区。下面是删节上市作为一个单一的脚本,例如:

I cannot see the result that you claim, which means it works for me and the rest of the world. Here is the abridged listing as a single script example:

    var async = require('async'),
        mongoose = require('mongoose'),
        Schema = mongoose.Schema;

    mongoose.connect('mongodb://localhost/test');

    var invitationSchema = new Schema({
      "ID": { "type": Schema.Types.ObjectId },
      "Accepted": Boolean
    });

    var userSchema = new Schema({
      "Invitation": [invitationSchema]
    });


    var User = mongoose.model( 'User', userSchema );

    User.find({},'Invitation',function(err,docs) {
      if (err) throw err;
      var results = [];

      async.each(docs,function(doc,callback) {
        async.each(doc.Invitation, function(invite,callback) {
          User.findOneAndUpdate(
            { "_id": doc._id, "Invitation._id": invite._id },
            { "$set": { "Invitation.$.Accepted": !invite.Accepted } },
            function(err,doc) {
              results.push(doc);
              callback(err);
            }
          );
        },callback);
      },function(err) {
        if (err) throw err;
        console.log( results.slice(-1)[0] );
        process.exit();
      });
    });

因此​​,明确切换的要求和完美的作品都值。

So that clearly "toggles" both values as requested and works perfectly.

这是从我上一炮打响的结果是:

This is the result from me on one shot:

{ _id: 54be2f3360c191cf9edd7236,
  Invitation:
   [ { __v: 0,
       ID: 54afaabd88694dc019d3b628,
       __t: 'USER',
       _id: 54b5022b583973580c706784,
       Accepted: true },
     { __v: 0,
       ID: 54af6ce091324fd00f97a15f,
       __t: 'USER',
       _id: 54bde39cdd55dd9016271f14,
       Accepted: true } ] }

这篇关于子文档切换布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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