0

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 Answer 1

1

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'.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that worked. Thanks. Did not think I could use single quotes. Learned something today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.