Search code examples
mongodbmgo

Creating a query with mongoDB and mgo


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!


Solution

  • Assuming your collection is named users.

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

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