0

I prepare query with match condition, and use $nin. I need select all document where status not equal 0,5,50,1. Maybe I'am not correct understand $nin operator. $nin - the field value is not in the specified array or. It's means not equal 5, or not equal 50, but value 5 not equal 50 and this documant satisfies.... But it's not make sense. Which operator implement strong condition - not equal elements from array (like and) This is my query

 db.getCollection('inv').aggregate(
  [    
     { $match: { $and: [ { status: {$nin: [0, 5, 50, 1]}} ] } },
     { $sort: { increment_id: -1 } },
     { $limit: 5 },
     {
         $project:
           {
             status: 1,  
             item: 1,
             entity_id: 1,
             increment_id: 1,  
             due_date: 1,  
             current_date: new Date(),   
             type_code: 1,    
             compare_duo_date:
               {
                 $cond: { if: { $lt: [
                     { $dateFromString: {
                 dateString: {$dateToString: { format: "%Y-%m-%d", date: "$due_date" }} } }, 
                 { $dateFromString: {
                 dateString: {$dateToString: { format: "%Y-%m-%d", date: new Date() }} } } 
                 ] }, then: 1, else: 0 }
               }            
           }
      }
   ]
)
shuba.ivan
  • 3,824
  • 8
  • 49
  • 121
  • 2
    When you say something does not work you are actually expected to show some proof. Show documents you think should not be returned and some that should, and then show us your query results clearly showing the ones you think that should not be returned in the result. We basically need a "small sample" of data in order to reproduce this. For the record though, **works as designed**. So what we are dealing with here is a mistake, or not reading the fine print in the documentation. My bet is that `status` actually contains "strings". – Neil Lunn Mar 25 '19 at 08:41
  • you absolutly right, `type string`, when use elements from array `string` everything work. Thnks you – shuba.ivan Mar 25 '19 at 08:44
  • Let's call it a typo then. You're probably using mongoose so you never noticed the difference. But aggregation pipelines require you to cast to the correct types. Mongoose cannot typecast there. Even though I have long argued that "technically" as long as the first pipeline stage is a `$match` then typecasting should really be attempted there. – Neil Lunn Mar 25 '19 at 08:48

0 Answers0