0

Let me be straight with the question, OK I have document structure like

let the collection name be sample;

were i'll not be knowing the someIdAsString.

{
_id : ObjectId("someObjectId"),
test : {
    someIdAsString : {
        field1 : value1,
        field2 : value2
    },
            someOtherIdAsString : {}
            .....
            .....
            And so on
  }
}

I have the field1 & field2 values how do i query the collection using this information.

field1 & field2 will be known at run time lets say it's field1 = '33' & field2 = '3333' and the document like

{
_id : ObjectId("522182515f8wea670900001b"),
test : {
    522182515f8c4wd70900001b : {
    field1 : '33',
    field2 : '3333'
    },
        522182515f8cea670900001b : {}
        .....
        .....
        And so on
   }
}

Thanks

Beast
  • 617
  • 2
  • 8
  • 19
  • 1
    Please give one example with the actual collection data and an expected result. – H.D. Aug 31 '13 at 06:27
  • A part of the program which actually does the query, would make things clearer. – thefourtheye Aug 31 '13 at 06:35
  • field1 & field2 will be known at run time lets say it's field1 = '33' & field2 = '3333' and the document like `{ _id : ObjectId("5221828c5f8cea670900as1d"), test : { 5221828c5f8cea670900001d : { field1 : '33', field2 : '3333' }, 5221828c5dscea670900001d : {} ..... ..... And so on } },` – Beast Aug 31 '13 at 06:36

1 Answers1

1

I suggest you to change the schema, so that this someIdAsString text becomes a value instead of a key, making the object in test become a list of objects. If you know every key, you can try

db.sample.find({$or: [
    {"test.someIdAsString.field1": value1,
     "test.someIdAsString.field2": value2},
    {"test.someOtherIdAsString.field1": value1,
     "test.someOtherIdAsString.field2": value2},
    ...
]})

For all your "someIdAsString" possibilities.

If you change the structure to:

{
    _id : ObjectId("someObjectId"),
    test : [
        {
            _id : someIdAsString,
            field1 : value1,
            field2 : value2
        },
        {
            _id : someOtherIdAsString,
            field1 : value3,
            field2 : value4
        },
        ...
    ]
}

You can change the query to:

db.sample.find({
    "test.field1": value1,
    "test.field2": value2
})

A workaround to use a query while still using your data structure without knowing the embedded key names would be a JavaScript in a $where operator.

db.sample.find(function(){
    var ttk;
    var tt = this.test;
    for(key in tt){
        ttk = tt[key]
        if(ttk.field1 === value1 && ttk.field2 === value2){
            return true;
        }
    }
    return false;
})
H.D.
  • 4,168
  • 1
  • 18
  • 15
  • Thanks @H.D. for your time, I wanted to know whether there is a way to query using mongoskin itself, but i think i found the answer and it is no i guess. – Beast Aug 31 '13 at 06:59
  • The function in my last example can be used like a `{$match: function(){...}}`, or a `{$match: "(function(tt){...})(this.test)"}` query, which you probably can use inside a `collection.find` from mongoskin. – H.D. Aug 31 '13 at 07:09