Minimongo还没有在预测中支持$ operator

时间:2015-04-30 13:13:00

标签: mongodb meteor

我有这个文件:

...
    "username" : "torayeff",
    "profile" : {
        ...
        "friends" : [
            {
                "_id" : "aSD4wmMEsFnYLcvmP",
                "state" : "active"
            },
            {
                "_id" : "ShFTXxuQLAxWCh4cq",
                "state" : "active"
            },
            {
                "_id" : "EQjoKMNBey7WPGamC",
                "state" : "new-request"
            }
        ]
        ...
    }
...

这是仅获取给定用户的"州" 属性的查询:

Meteor.users.findOne({_id: userId1, 'profile.friends._id': userId2}, 
                    {fields: {_id: 0, 'profile.friends.state.$':1} });

在meteor中我收到此错误:

Exception while simulating the effect of invoking 'activateFriendship' Error: Minimongo doesn't support $ operator in projections yet...

如何在minorongo中没有错误的情况下重写上述查询?

2 个答案:

答案 0 :(得分:-1)

使用$elemMatch查找嵌套数组,并在投影中使用$,因此查询如下:

Meteor.users.findOne({
  "_id": userId1,
  "profile.friends": {
    "$elemMatch": {
      "_id": userId2
    }
  }
}, {
  "profile.friends.state.$": 1
})

答案 1 :(得分:-1)

你可以这样做

编辑:如果您只想获取没有键的值,请使用此

Meteor.users.distinct('profile.friends.state', {_id: userId1, 'profile.friends._id': userId2});

通常情况:

Meteor.users.findOne({_id: userId1, 'profile.friends._id': userId2}).select({ _id: 0, 'profile.friends.state':1 }).exec()

$项目运营商不需要

相关问题