【问题标题】:Mongoose find one query on object under objectMongoose 在对象下的对象上找到一个查询
【发布时间】:2018-03-08 01:02:27
【问题描述】:

我有用户架构

var userSchema = new Schema({
     userId          : {type:String,default:'',required:true},
    firstName       : {type:String,default:'',required:true}, 
    lastName        : {type:String,default:'',required:true}, 
    email           : {type:String,default:'',required:true},
    password        : {type:String,default:'',required:true}, 
    mobileNumbers   : {}, 
    recoveryDetails : {}, 
    walletInfo      : {}, 
    savedAddress    : {}, 
    savedCards      : {}, 
    cart            : {
        productId              :  {type:String,default:'',required:true},
        productName            :  {type:String,default:'',required:true}, 
        productPrice           :  {type:String,default:'',required:true},
        productDescription     :  {type:String,default:'',required:true},
        productSpecifications  :  {}, 
        productSeller          :  {}, 
        productImages          :  [], // An array to store product images //
        productCategory        :  []  // Product categories array //

    }   // Shopping cart array of user //
});

我想查找带有产品 ID 的产品。我正在编写类似

的查询
  userModel.findOne({'user.cart': {$elemMatch: {productId: productsId}}},function(err,productFoundInUserCart){
});

这是正确的方法吗?不在我这边工作。谁能帮我解决这个问题?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    你的查询有误,试试这个

    userModel.findOne({'cart.productId': productsId},function(err,productFoundInUserCart){
    });
    

    【讨论】:

      猜你喜欢
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2013-06-03
      • 2021-11-06
      • 1970-01-01
      • 2022-10-25
      • 2018-04-25
      相关资源
      最近更新 更多