0
votes

I've been struggling for hours. Here's what I need to achieve: find if document exists where both conditions are satisfied: Field1 = 'Value1' and users.userName = 'Value2'

{
  "_id": "eRotXZoKL4aS3KygF",
  "Field1": "Value1",
  "usercount": 1,
  "owner": "Admin",
  "accessType": "Level1",
  "users": {
    "userName": "Value2",
    "expired": false
  }
}

My query

Collection.findOne({},{Field1: channel, users:{userName: nickName}});

returns first match it finds. In other words, it finds the document where users:{userName: 'Value2'}, but disregards, or so it seems Field1: 'Value1'

1

1 Answers

1
votes

The first parameter to findOne is the selector. Try this:

Collection.findOne({Field1: channel, 'users.userName': nickName});

Mongo uses an implicit AND between all fields in the selector object. Also note that to match a nested object you'll use dot notation to reference embedded documents - in this case, 'users.username'.