Using dot notation for keys when retrieving subdocuments

I use this scheme to store some documents:

{
  "_id": ObjectId("..."),
  "param0": "value0",
  "status": {
    "param1": "value1",
    "param2": "value2",
    "param3": "value3"
  }
}

.. and when I request for certain fields inside subdocument

db.collection.find({}, {"param0": 1, "status.param1": 1, "status.param3": 1})

.. it returns nested object as it's actually used:

{
  "_id": ObjectId("..."),
  "param0": "value0",
  "status": {
    "param1": "value1",
    "param3": "value3"
  }
}

Is it possible to retrieve it like this?

{
  "_id": ObjectId("..."),
  "param0": "value0",
  "status.param1": "value1",
  "status.param3": "value3"
}

Answers


You can very close to what you want if you use the aggregation framework to transform your document:

db.collection.aggregate({
    $project:{
        param0: 1,
        status_param1: "$status.param1",
        status_param3: "$status.param3"
    }
})

This will give you a result like this:

{
"result" : [
    {
        "_id" : ObjectId("5207908547bbc806a99c5198"),
        "param0" : "value1",
        "status_param1" : "value1",
        "status_param3" : "value3"
    }
],
"ok" : 1
}

You should, hopefully, be able to work with this type of result.

More information about mongodb project aggregator


Yes it is, have a look at the docs.

The example took from there reflects your need:

db.inventory.find({
  producer: {
    company: 'ABC123',
    address: '123 Street'
  }
})

db.inventory.find({'producer.company': 'ABC123'})

Need Your Help

Return value from exec(@sql)

sql sql-server sql-server-2005 tsql

I want get the value from Exec(@sql) and assign to @Rowcount(int)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.