A
A
Artur Kogut2015-08-19 18:53:42
NoSQL
Artur Kogut, 2015-08-19 18:53:42

In MongoDB, is it possible to query a property (individual value) of a document hierarchy only by querying the whole document, or is it possible in parts, with any nesting level?

In MongoDB, is it possible to query a property (individual value) of a document hierarchy only by querying the whole document, or is it possible in parts, with any level of nesting?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Илья Шатохин, 2015-08-19
@iShatokhin

Да, можно.
При записях в базе вида:

{
  "_id": ObjectId("530824b95f44eac1068b45c8"), 
  "accs": {
    "boss": {
      "name": "Stefan",
      "roles": ["admin", "ceo"]
    }
  },
  "ingener": {
    "name": "Jhon",
    "roles": ["user", "staff"]
  }
}

Можно сделать запрос db.collection.find({},{ "accs.boss.roles": {$slice: -1} }) и получить:
{ 
  "accs": {
    "boss": {
      "roles": ["ceo"]
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question