1

I'm just started to create my MongoDB schema (im newbie on mongo), my schema looks like this example:

users : [
    {
        _id : 11,
        name : "jhon",
        mail : "jhon@mail.com"
    },
    {
        _id : 12,
        name : "smith",
        mail: "smith@mail.com"
    }
]

I want to retrieve certain user information, bsaed on a _id... For example:

db.users.find({"users._id" : 11})

And then my result should be:

_id : 11,
name : "jhon",
mail : "jhon@mail.com"

but the result i get is all the users of the users document, i just one one, the one who have certaint _id.

How can i write a query for that?. And just in case, my schema is designed properly?

Thanks for the help! :)

Jesús Cota
  • 515
  • 1
  • 4
  • 14
  • In your example, are you showing your `users` collection as an array or does your `users` collection only contain one doc, which also has a field named `users`? – JohnnyHK Feb 26 '14 at 02:04
  • My main collections it's called `users`, inside i have a document called `users` too.. Inside `users` array i want to store a unknown number of users, then i want to query for a user using it´s `_id`.. I don't know if is that possible? – Jesús Cota Feb 26 '14 at 02:43
  • See this question: http://stackoverflow.com/q/3985214/1259510 – JohnnyHK Feb 26 '14 at 02:50
  • Ok, know im getting the result i want.. But just a little question, can you please exmplain why is this part used? `{"_id" : 0, 'users.$' : 1}` ... Thanks for the answer :D – Jesús Cota Feb 26 '14 at 03:49
  • That's the projection parameter used to suppress the `_id` field in the output and include just the matched `users` element. – JohnnyHK Feb 26 '14 at 21:40
  • Ok, that's perfect, thank you very much and im done with this question. – Jesús Cota Feb 26 '14 at 22:27

2 Answers2

1

Try with this

db.users.find({"_id" : 11})
Rafa Paez
  • 4,820
  • 18
  • 35
0

You ask whether your schema is designed properly. That of course depends on your use case, but the answer is probably... no. Instead of a single document with an array, probably what you want is multiple documents. For example, I inserted some documents using the mongo shell:

> db.users.insert({_id: 11, name: "jhon", mail: "jhon@mail.com"})
> db.users.insert({_id: 12, name: "smith", mail: "smith@mail.com"})

Then here's a query typed at the mongo shell that will return all documents:

> db.users.find()
{ "_id" : 11, "name" : "jhon", "mail" : "jhon@mail.com" }
{ "_id" : 12, "name" : "smith", "mail" : "smith@mail.com" }

And if you have the _id, you can query just that document:

> db.users.findOne({_id: 11})
{ "_id" : 11, "name" : "jhon", "mail" : "jhon@mail.com" }

Arrays have their uses, but the kind of thing you are doing here is usually done with multiple documents in a collection, rather than sub-documents within an array.

Does this help?

Bruce Lucas
  • 949
  • 5
  • 5
  • A lot, because im new to mongo and i think i don't want to mess with arrays at the moment. Thanks for your answer :) – Jesús Cota Feb 26 '14 at 22:27