0

This is the JSON structure, note that the last entry has 2 multidimensional arrays in it, one for chr 10 and one for chr 12:

{
  "_id": "9oFvJYeG9wpdBYunu",
  "segments": [
    {
      "chr": "7",
      "start": "140422294",
      "end": "155048283",
      "length": "29.1",
      "snps": "1666"
    }
  ]
},
{
  "_id": "HK4WXc5mR6fyesjpP",
  "segments": [
    {
      "chr": "10",
      "start": "83865742",
      "end": "90981118",
      "length": "6.3",
      "snps": "1380"
    }
  ]
},
{
  "_id": "3N4Z2dtX5PiuqmCFv",
  "segments": [
    {
      "chr": "10",
      "start": "83865742",
      "end": "90981118",
      "length": "6.3",
      "snps": "1380"
    },
    {
      "chr": "12",
      "start": "32853998",
      "end": "44834540",
      "length": "5.1",
      "snps": "1623"
    }
  ]
}

How can I identify all segments with chr = 10? I want to get the detailed information about the elements with chr = 10. I want exactly those elements with chr = 10, not give me all documents (with all other their segments) where one of the elements has chr = 10.

Please further note that according to the MongoDB documentation both '$elemMatch' and '$' only return the first match. However my collection has documents where there are two or more elements with chr = 10. I want to get all elements, also in this case with multiple chr 10 elements in one array.

I want to return the following fields:

_id, chr, start, end, length, snps

back. How can I query this in Meteor? I tried $elemMatch an $in but to no avail. If you only answer this for MongoDB please do so, I will try to convert it into Meteor Javascript.

Thanks in advance for your help!

P.S.: this is on the Meteor server side, not sure if that's important

a4xrbj1
  • 437
  • 3
  • 18
  • 2
    So you do not want `db.coll.find({segments: {$elemMatch: {chr: "10"}}})`? – serv-inc Oct 01 '15 at 17:54
  • 2
    This may be of help http://stackoverflow.com/questions/3985214/ – chridam Oct 01 '15 at 17:55
  • 1
    Have you seen http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection? – serv-inc Oct 01 '15 at 17:56
  • In principle I want this but it also returns all other elements in case a document has more than one element. I want exactly those elements with chr = 10, not give me all document where one of the elements has chr = 10. Sorry, should have made that clearer. – a4xrbj1 Oct 01 '15 at 18:59

1 Answers1

-1

you can try find with projection

Query

db.collection.find({"segments.chr":"10"},{"segments.$":1}).pretty()

Output

{
        "_id" : "HK4WXc5mR6fyesjpP",
        "segments" : [
                {
                        "chr" : "10",
                        "start" : "83865742",
                        "end" : "90981118",
                        "length" : "6.3",
                        "snps" : "1380"
                }
        ]
}
{
        "_id" : "3N4Z2dtX5PiuqmCFv",
        "segments" : [
                {
                        "chr" : "10",
                        "start" : "83865742",
                        "end" : "90981118",
                        "length" : "6.3",
                        "snps" : "1380"
                }
        ]
}

Hope it will help

Rohit Jain
  • 1,723
  • 12
  • 17
  • Used db.matches.find({ "segments.chr": "10" }, { "kit1":1, "segments.$":1 }).sort({"kit1":1}) as kit1 (key) identifies a document with 2+ elements in the array. It does find both elements but the values are identical. See here: { "_id" : "ZtojR5BLoFHGoXCih", "kit1" : "F393988", "segments" : [ { "chr" : "10", "start" : "34068234", "end" : "50002936", "length" : "7.2", "snps" : "895" } ] } { "_id" : "SdhYw8gR4m9CWNHY6", "kit1" : "F393988", "segments" : [ { "chr" : "10", "start" : "34068234", "end" : "50002936", "length" : "7.2", "snps" : "895" } ] } – a4xrbj1 Oct 01 '15 at 19:23
  • @a4xrbj1 - can you tell us what you want, the above ans based upon your original post. Please don't confuse us – Rohit Jain Oct 01 '15 at 19:28
  • has nothing to do with confusing you but there are 44000 documents in the database currently, which is impossible to show in the example. I showed in my question what data structure I want to be returned and that I ONLY want the elements from an array that qualify the query of "chr = 10", not any that isn't matching. If an array has more than one element with matching criteria than I want get all those elements back. – a4xrbj1 Oct 02 '15 at 05:23
  • 1
    Can you have a look http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/ hope it will help. Cheers Rohit – Rohit Jain Oct 02 '15 at 13:17