本文介绍了带位置运算符的MongoDb拉取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文档结构如下

{
 _id: 12342342
 items: [
   {
      ownerId: 123,
      dates: ['2014-10-01', '2014-10-02']
   },
   {
      ownerId: 234,
      dates: ['2014-10-01', '2014-10-02']
   },

 ]
}

我想从父对象的ownerId是特定值的日期中提取特定值.

I want to pull a specific value from dates where the ownerId of the parent object is a specific value.

我有

   var myDate = '2014-10-01'
   db.collection('things').update({'items.dates': {$in: [myDate]}},
      {$pull: { 'items.$.dates': myDate } }, {multi: true});

但是这将拉动任何ownerId,这不是我想要的.有人可以帮助我获得正确的查询语法吗?

But this will pull for any ownerId, which is not what I want. Can someone help me get the right query syntax?

谢谢.

推荐答案

要在特定的 ownerId 拉动,您需要指定 ownerId update 方法的查询部分中.这是给您的参考:

As you want to pull on specific ownerId, you need to specify that ownerId in the query part of update method. This is a reference for you:

var myDate = '2014-10-01';
var ownerId = 123;

db.things.update({
    'items' : {
        '$elemMatch' : {
            ownerId : ownerId,
            dates : {
                $in : [ myDate ]
            }
        }
    }
}, {
    $pull : {
        'items.$.dates' : myDate
    }
}, {
    multi : true
});

实际上,这里不需要运算符 $ in .

Actually, operator $in is unnecessary here.

var myDate = '2014-10-01';
var ownerId = 123;

db.things.update({
    'items' : {
        '$elemMatch' : {
            ownerId : ownerId,
            dates : myDate
        }
    }
}, {
    $pull : {
        'items.$.dates' : myDate
    }
}, {
    multi : true
});

这篇关于带位置运算符的MongoDb拉取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 05:59