MongoDB order by

0

practicing with mongo

I have a collection

   {
    "_id" : "158748",
    "data" : {
        "1" : {
            "tem" : 0,
            "sal" : 0,
        },
        "3" : {
            "tem" : 0,
            "sal" : 0,
        },        
       "4" : {
            "tem" : 0,
            "sal" : 0,
        },
        "2" : {
            "tem" : 0,
            "sal" : 0,
        }
    }
}

and to sort it in the find () query

db.getCollection('data').find({'_id':158748}).sort({"data":1 });

does not work, I saw another one

db.getCollection('data').find({$query:{'_id':158748}, $orderby:{"data":1 }});

but gives this error

Error: error: {
    "waitedMS" : NumberLong(0),
    "ok" : 0,
    "errmsg" : "unknown top level operator: $query",
    "code" : 2
}

I do not know how it could be done so that as a result of the find with the values of "data" computer by its key

    
asked by jonellrz 20.11.2016 в 11:01
source

1 answer

0

The problem you have is that you are writing the query incorrectly. Get this and tell me if it works for you:

db.getCollection('data').find({"_id":"158748"}).sort({"data":1 });

If you realize, you are looking for {"_id":158748} and where I have spent is {"_id":"158748"} .

    
answered by 21.11.2016 в 00:56