2017-10-01 1 views
1

Désolé, la première fois en essayant mongo.Collection de requêtes contenant BsonArray

Compte tenu des données suivantes ...

db.masterList.findOne() 
{ 
     "_id" : ObjectId("59d128805b19310ac8ab3fc2"), 
     "MasterDefinition" : { 
       "Location" : [ 
         "Whole House", 
         "Master Bedroom", 
         "Hallway 2" 
       ], 
       "DeviceType" : [ 
         "Receptacle", 
         "GFI", 
         "LED dimmer" 
       ], 
       "Style" : [ 
         "Decora", 
         "Standard" 
       ], 
       "Color" : [ 
         "White", 
         "Light Almond" 
       ] 
     } 
} 

Comment puis-je récupérer le contenu du tableau Color? Je me attends quelque chose comme

["White","Light Almond"] 

Comment puis-je énumérer les 4 tableaux directement subordonnés à MasterDefintion? Je me attends à

["Location","DeviceType","Style","Color"] 

Merci

+0

"Compte tenu de la collection suivante ..." avez-vous l'intention d'ajouter quelques détails collection à votre question? – glytching

+0

Pourriez-vous mettre à jour votre question avec (a) ce que vous avez essayé jusqu'à présent et (b) votre résultat ** attendu **. Compte tenu d'une déclaration claire de la sortie que vous recherchez, il y aura probablement une réponse simple et rapide. – glytching

Répondre

0

Pour la première partie, vous pouvez simplement faire un

collection.aggregate({ 
    $project: { 
     "_id": 0, // exclude the "_id" field from the result 
     "result": "$MasterDefinition.Color" 
    } 
}) 

La deuxième partie a besoin d'un peu de magie (documentation se trouve ici: aggregation framework, $project ($objectToArray):

collection.aggregate({ 
    $project: { 
     "temp": { 
      $objectToArray: "$MasterDefinition" // transform the "MasterDefinition" subdocument into an array 
     } 
    } 
}, { 
    $project:{ 
     "_id": 0, // do not include the "_id" field in the result - this is an optional step 
     "result": "$temp.k" // only get the keys (as in "k" fiels) from the array 
    } 
}) 
0

Comment puis-je récupérer le contenu de la matrice de couleurs? Je me attends quelque chose comme [ "blanc", "Lumière d'amande"]

// the first argument here is a filter, the second argument is a projection 
// since you specified no filter I have only included a projection 
// this projection tells MongoDB to return the Color subdocument 
// from within the MasterDefinition sub document 
db.getCollection('masterList').find({}, {'MasterDefinition.Color': 1}) 

La commande ci-dessus retourne:

{ 
    "_id" : ObjectId("59d128805b19310ac8ab3fc2"), 
    "MasterDefinition" : { 
     "Color" : [ 
      "White", 
      "Light Almond" 
     ] 
    } 
} 

Comment puis-je lister les 4 tableaux directement subordonnés à MasterDefintion? Je me attends à [ "Location", "DeviceType", "Style", "Couleur"]

Ceci est un peu plus compliqué parce que "Localisation", "DeviceType", "Style", "Couleur" ne sont pas Les éléments d'un tableau sont plutôt les noms des attributs du sous-document MasterDefinition. Vous pouvez utiliser le $objectToArray aggregation operator pour transformer ces noms d'attribut en un tableau, mais le document qui en résulte ne ressemble pas exactement à ce que vous espériez. Voici un exemple ...

db.getCollection('masterList').aggregate([ 

    // creates an array named "categories" from the attributes of the MasterDefinition sub document 
    { $project: { categories: { $objectToArray: "$MasterDefinition" } } }, 

    // projects on the keys of the "categories" array 
    {$project: {'categories.k': 1}} 

]) 

... qui produit cette sortie:

{ 
    "_id" : ObjectId("59d128805b19310ac8ab3fc2"), 
    "categories" : [ 
     { 
      "k" : "Location" 
     }, 
     { 
      "k" : "DeviceType" 
     }, 
     { 
      "k" : "Style" 
     }, 
     { 
      "k" : "Color" 
     } 
    ] 
}