0

I have a collection of mongoDB entries like the one below...

{
  "_id": ObjectId("4e2a4ca7f21a81331f0006c3"),
  "users": {
    "bob": 1375496448, "alice": 1375496448
  },
  ...other values...
}

I am looking for a simple query for me to find all entries...
1) Without user x in users
2) With user x in users where the corresponding value is < y

I hope this question is not too trivial, but I just started learning mongoDB this afternoon and I would like to get it up and running in a simple server I am planning to set up. Thanks!

2
  • Could you please provide more info on the collection. It's hard to query the collection without a better idea of the collections structure. Cheers Commented Aug 3, 2013 at 8:11
  • The struct I am using for this collection is Address string, Port string, Timestamp int, Users map[string]int I need to have a query to return the union of the 2 cases listed above. I hope this helps! :D Commented Aug 3, 2013 at 23:02

2 Answers 2

2

Assuming your collection is named users.

1)db.users.find({'users.X':{$exists:false}})

2)db.users.find({'users.X':{$lt:y}})

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

1 Comment

Is there anyway to take the UNION of both results? How would I structure that in mgo syntax?
2

Ishaan's answer should work for you. I am only adding a way to Union the two queries.

db.users.find( {$or: [ {'users.X':{$exists:false}}, {'users.X':{$lt:y}} ] } )

You will find documentation of operators like $or, $and, etc. on this page.

Comments

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.