在Mongodb集合数组字段上查找元素

时间:2017-05-28 18:53:24

标签: node.js mongodb express

我的usuario集合:

{ "_id" : ObjectId("5817a06de4e150a1418b4567"),  
  "idStore" : [
"58177ca2e4e15041058b4567", 
"5817a192e4e150d7098b4567",         
"5817bbb3e4e15020778b4567"
]}

db.collection('atendimento').aggregate([{ $match: { dataI:{ $gt: fDate },     status: 'OCUPADO' } }, 
            { $group: {_id: {"idLoja":"$idLoja", "venda":"$sim"},  qtd:     {"$sum":1}} },  
             {$sort: {'_id.idLoja': 1, '_id.venda': 1}}]).toArray(function(err,     result) {
            var vIdLoja = '';
            for (var x = 0; x < result.length; x++) {   
                vIdLoja = result[x]._id.idLoja;
.
.
.
    db.collection('usuario').find({idStore: { $elemMatch: { $gte: vIdLoja, $lte: vIdLoja } }}).toArray(function(err, result) {

或:

db.collection('usuario').find({idStore: vIdLoja}).toArray(function(err, result) {

结果相同:empty/undefined driver:MongoClient

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

由于idStore是一个简单的字符串数组,您可以直接查询该字段:

db.collection('usuario').find({ idStore: "5817a192e4e150d7098b456" }, ...);