2017-09-20 2 views
0
var otherLanguages=[ "English","Arabic","French"]; 

var first, second; 
db.collection.find({ $and: [ { "Language" : { $nin : otherLanguages} },{"Language":{ $ne:null}} ]}).forEach(function(obj){ 

annulez 341 documents un par un. Dans ces documents, je veux trouver des documents qui satisfont deux déclarations if. Plus tard, je veux récupérer le compte.Comment rechercher des enregistrements dans un objet bson

if (obj.find({ $and: [{'POS': { $eq: "Past" } },{'Desp': { $ne: null } }] })) { first= first+1;} 
if (obj.find({ $and: [{'POS': { $eq: "Past" } },{'Desp': { $eq: null } }] })) {second= second+1;} 
    }); 
    print (first,second) 

Je sais que je ne peux pas utiliser find() fonction de l'obj, mais est-il un moyen de recherche sur ce « BSON obj » pour trouver le comte. Si cela n'est pas possible, veuillez suggérer un moyen d'obtenir le résultat souhaité.

Répondre

0

Si je comprends bien votre question, vous pouvez y parvenir en utilisant le cadre d'agrégation comme ceci:

db.collection.aggregate({ 
    // filter out all documents that you don't care about 
    $match: { 
     "Language": { $nin: otherLanguages, $ne: null }, 
     "POS": "Past" 
    }, 
}, { 
    // then split into groups... 
    $group: { 
     _id: { $eq: [ "$Desp", null ] }, // ...one for the "eq: null" and one for the "ne: null" 
     "count": { $sum: 1 } // ...and count the number of documents in each group 
    } 
})