1

Consider my query to be: {cheese:"Cheddar"} and I have the following collections: {vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Blue"}, {milk:"Chocolate}, {cheese:"Cheddar"}

How do I make a find that returns me all collections that include cheese:Cheddar? The result would be {vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Cheddar"} but right now it fives me just {cheese:"Cheddar"}. From what I investigated I only found tokens to work with arrays.

I do NOT know the name of the property is cheese, nor do I know if there are any other ingredients.

I am looking for a way to get documents from a collection, where the query is included in a field, by the names of the properties in the query and the respective values.

Discipol
  • 3,137
  • 4
  • 22
  • 41
  • When you say collections in the first line, do you mean documents? If you want to find documents from different collections you should do multiple queries. – gtsouk Apr 28 '13 at 21:49

1 Answers1

1

Using db.collection.findOne({cheese:"Cheddar"}) you will get as a result only one document, maybe {cheese:"Cheddar"} or maybe {vegetable:"Lettuce", cheese:"Cheddar"}, the first one that MongoDB finds depending on the _id field. If what you want is getting both, you should use db.collection.find({cheese:"Cheddar"}).

Lucia Pasarin
  • 2,268
  • 1
  • 21
  • 37
  • My bad, i copy pasted it wrong, i used find in my code. It returns only one result. I may have oversimplified my example, as the recipes are a property of the collections and I am using find( {ingredients:{cheese:"Cheddar"}}) and you have collections like {_id:0,ingredients:{vegetable:"Lettuce", cheese:"Cheddar"}}. I'm new to this and I will feel embarrassed to see the solution. I understand its trying to return to me an accurate query but I am looking for a partial hit on the find, basically. – Discipol Apr 28 '13 at 21:49
  • 1
    Ok, then in this case, what you should use is `db.collection.find({"ingredients.cheese":"Cheddar"})` – Lucia Pasarin Apr 28 '13 at 21:52
  • Right but I don't know its called cheese and might be 2 ingredients. Basically if my query is included by property names and their respective values, return me that collection(s). Would this description spark a solution? – Discipol Apr 28 '13 at 21:55
  • If your documents have more ingredients than cheese, this will still give you the right result, you should simply be sure that the field that you want is called cheese. – Lucia Pasarin Apr 28 '13 at 21:59
  • 1
    I am not sure you understand the difference between documents and collections. By the example you are giving you should only have one collection, the collection of reciepies – gtsouk Apr 28 '13 at 22:00
  • you are right gtsouk. Allow me to recap. I am looking for a way to get documents from a collection, where the query is included in a field, by the names of the properties in the query and the respective values. – Discipol Apr 28 '13 at 22:01
  • excuse me but my question is not answered, the duplicate implies I know the properties' names, which I do not. – Discipol Apr 28 '13 at 22:10
  • You should know the properties' names in advance to know what you want to get as a result. Why don't you know them? – Lucia Pasarin Apr 28 '13 at 22:14
  • I get a json with grocery list and I need all recipes that include those particular ingredients in name and quantity(for each). It would be silly to find() all documents and filter them clientside – Discipol Apr 28 '13 at 22:16
  • How does your json look like? Do you get from it information about if your ingredient is a vegetable, cheese, etc. or not? – Lucia Pasarin Apr 28 '13 at 22:21
  • the ingredient's name is "vegetable" so you know its a vegetable. – Discipol Apr 29 '13 at 06:59
  • I would advise you to ask a new question explaining this concrete requirement that you have for getting data from a json in a clearer way :) – Lucia Pasarin Apr 29 '13 at 17:45