Minimongo does not yet support $ operator in forecasts

I have this document:

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

This is a request to get only the "state" attribute of this user:

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

In the meteor, I get this error:

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

How can I rewrite the above query without error in minimongo?

+4
source share
2 answers

Use $ elemMatch to search for a nested array and $ in your projection, so query as below:

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

you can do as

EDIT: if you just want to capture values ​​without keys use this

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

For ordinary cases:

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

$ project operator not required

-1
source

All Articles