如何在MongoDB查询中将$elemMatch与$nin或$not组合?

如何在MongoDB查询中将$elemMatch与$nin或$not组合?,mongodb,mongodb-query,Mongodb,Mongodb Query,我想找到数组fieldB在fieldC对象中没有值(val1和val2)的所有结果 我试过: db.SalesToImport .find( { fieldA: {$exists:true}, ,fieldB: {$not: {$elemMatch: {fieldC: 'val1'}}} ,fieldB: {$not: {$elemMatch: {fieldC: 'val2'}}} } ); db.SalesToImport .f

我想找到数组fieldB在fieldC对象中没有值(val1和val2)的所有结果

我试过:

db.SalesToImport
  .find(
    {
      fieldA: {$exists:true},
      ,fieldB: {$not: {$elemMatch: {fieldC: 'val1'}}}
      ,fieldB: {$not: {$elemMatch: {fieldC: 'val2'}}}
    }
  );
db.SalesToImport
  .find(
    {
      fieldA: {$exists:true},
      ,fieldB: {$not: {$elemMatch: {fieldC: 'val1', fieldC: 'val2'}}}
    }
  );
我试过:

db.SalesToImport
  .find(
    {
      fieldA: {$exists:true},
      ,fieldB: {$not: {$elemMatch: {fieldC: 'val1'}}}
      ,fieldB: {$not: {$elemMatch: {fieldC: 'val2'}}}
    }
  );
db.SalesToImport
  .find(
    {
      fieldA: {$exists:true},
      ,fieldB: {$not: {$elemMatch: {fieldC: 'val1', fieldC: 'val2'}}}
    }
  );
它们都产生相同的结果,虽然它们删除了一些文档,但其中仍然有
val1
val2
的文档,这表明我的查询不正确

文档结构包含如下字段:

{
  fieldA: value,
  fieldB: [
    {fieldC:value1, fieldD:value3, fieldE:value5},
    {fieldC:value2, fieldD:value4, fieldE:value6}
  ]
}
我应该如何在MongoDB查询中将$elemMatch与$nin或$not组合以获得所需的效果

更新#1 根据这些建议,我运行了以下真实但经过净化的查询:

db.SalesToImport
  .find(
    {
      calculatedTaxRate: {$exists:true}, unidentifiedProducts: {$exists:false}, unidentifiedCustomer: {$exists:false},
      register_sale_payments: {
        $elemMatch: {
          retailer_payment_type_id : {
            $nin: [
              '0af7b240-ab24-11e7-eddc-9fff6de134e9'
              ,'0af7b240-ab24-11e7-eddc-9fff69293826'
            ]
          }, 
        }
      }
    },
    {_id:0,status:0,customer_id:0,register_id:0}
  )
  .sort({"calculatedTaxRate":1});
db.SalesToImport
  .find(
    {
      calculatedTaxRate: {$exists:true}, unidentifiedProducts: {$exists:false}, unidentifiedCustomer: {$exists:false},
      'register_sale_payments.retailer_payment_type_id': {
        $nin: [
          '0af7b240-ab24-11e7-eddc-9fff6de134e9'
          ,'0af7b240-ab24-11e7-eddc-9fff69293826'
        ]
      }
    },
    {_id:0,status:0,customer_id:0,register_id:0}
  )
  .sort({"calculatedTaxRate":1});
但是在
fieldB.fieldC
register\u sale\u payments.retailer\u payment\u type\u id
中,响应仍然有不想要的结果,所以这不是我想要的,或者我在使用它时犯了错误

更新#2 根据这些建议,我运行了以下真实但经过净化的查询:

db.SalesToImport
  .find(
    {
      calculatedTaxRate: {$exists:true}, unidentifiedProducts: {$exists:false}, unidentifiedCustomer: {$exists:false},
      register_sale_payments: {
        $elemMatch: {
          retailer_payment_type_id : {
            $nin: [
              '0af7b240-ab24-11e7-eddc-9fff6de134e9'
              ,'0af7b240-ab24-11e7-eddc-9fff69293826'
            ]
          }, 
        }
      }
    },
    {_id:0,status:0,customer_id:0,register_id:0}
  )
  .sort({"calculatedTaxRate":1});
db.SalesToImport
  .find(
    {
      calculatedTaxRate: {$exists:true}, unidentifiedProducts: {$exists:false}, unidentifiedCustomer: {$exists:false},
      'register_sale_payments.retailer_payment_type_id': {
        $nin: [
          '0af7b240-ab24-11e7-eddc-9fff6de134e9'
          ,'0af7b240-ab24-11e7-eddc-9fff69293826'
        ]
      }
    },
    {_id:0,status:0,customer_id:0,register_id:0}
  )
  .sort({"calculatedTaxRate":1});

成功了

虽然您只对
fieldB
的一个字段感兴趣,但不需要使用
$elemmatch

db.SalesToImport.find(
{
  "fieldA": {$exists:true},
  "fieldB.fieldC": { $nin: ["val1", "va2"] }
}
);
例如,如果在
fieldB
中有一个fieldD,并且不希望fieldD也有值val3和val4:

db.SalesToImport.find(
{
  "fieldA": { $exists: true },
  "fieldB.fieldC": { $nin: ["val1", "va2"] },
  "fieldB.fieldD": { $nin: ["val3", "va4"] }
}
);
上面的查询不包括fieldC的文档,fieldD对也是这样:(val5,val3),因为我们分别查询fieldC和fieldD

如果要查询
字段b
字段c和字段d没有值对(val1,val3),(val1,val4),(val2,val3),(val2,val4):


上面的查询不排除filedC,fieldD对,比如:(val5,val3)

当您只对
fieldB
的一个字段感兴趣时,您不需要使用
$elemmatch

db.SalesToImport.find(
{
  "fieldA": {$exists:true},
  "fieldB.fieldC": { $nin: ["val1", "va2"] }
}
);
例如,如果在
fieldB
中有一个fieldD,并且不希望fieldD也有值val3和val4:

db.SalesToImport.find(
{
  "fieldA": { $exists: true },
  "fieldB.fieldC": { $nin: ["val1", "va2"] },
  "fieldB.fieldD": { $nin: ["val3", "va4"] }
}
);
上面的查询不包括fieldC的文档,fieldD对也是这样:(val5,val3),因为我们分别查询fieldC和fieldD

如果要查询
字段b
字段c和字段d没有值对(val1,val3),(val1,val4),(val2,val3),(val2,val4):


上述查询不排除filedC,fieldD对,如:(val5,val3)

我不知道在没有多行支持的情况下如何有效沟通,因此我更新了我的原始问题以回应您的回答。请看一看。没有多线支持,我不知道如何有效沟通,所以我更新了我的原始问题以回应您的回答。请看一看。