首页 文章

在mongodb中使用$和$ match匹配

提问于
浏览
24

我试图在MongoDB中使用以下查询但它没有运行 .

db.test.aggergate(
$match: {$and: [type: {$in: ["TOYS"]}, type: {$nin: ["BARBIE"]}, time: 
{$lt:ISODate("2013-12-09T00:00:00Z")}]}})

它表示无效字符“:” .

是否可以使用$和$ match?我在这个$或者$ match的论坛上看过一个例子,所以我认为这是可能的 .

提前感谢您的帮助和指导 .

4 回答

  • 57

    $和$ match的工作正常 .

    您的查询中存在语法错误 . 试试这个 .

    db.test.aggregate([
                       { 
                         $match: {
                              $and: [ 
                                  {type: {$in: ["TOYS"]}}, 
                                  {type: {$nin: ["BARBIE"]}}, 
                                  {time: {$lt:ISODate("2013-12-09T00:00:00Z")}}
                              ]
                         }
                       }
                      ])
    

    而对于你想要做的事情,你不需要 $and .

  • 5

    如果有人想拒绝我的答案,那没关系,这是一个缺乏研究的典型问题

    db.test.find( {$and: [ {"type": {$in: ["TOYS"]}}, 
                           {"type": {$nin: ["BARBIE"]}}, 
                           {"time": {$lt:ISODate("2013-12-09T00:00:00Z")}}
                         ]
    })
    

    AND与FIND一起使用,接收匹配数组(但它不是匹配指令)聚合框架用于完全不同的东西,就像单词所说的那样,用于聚合(count,sum,avg等值得分组或展开等)

  • 3
    {
      $match: {
        $or:[
          {'sender':sender, 'recipient':recipient},
          {'recipient':sender,'sender':recipient}
        ]
      }
    }
    

    使用$或

  • 0

    以下是我如何处理许多$以及$或

    User.aggregate([
     {
      $lookup:{
       from:'influencers',
       localField:'_id',
       foreignField:'user',
       as:'influencer'
     }
     },
     { $match:{ '$and': 
      [ { '$and': [ { 'influencer.aboutMe.birthday': { '$lte': 2017-09-11T09:10:07.283Z } } ] },
      { '$and': [ { 'influencer.aboutMe.minPrice': { '$gte': 0 } } ] },
       { '$and': 
        [ { '$or': 
            [ { '$and': [ { 'influencer.followed_by': { '$gte': 0 } } ] },
    
             { '$and': [ { 'influencer.youTubeSubscribercount': { '$gte': 
      0 } } ] } ] } ] },
     { 'influencer.isAdminverified': true },
     { 'influencer.status': 'Verified' } ] } }
    
    ]);
    

相关问题