Mongodb,在插入新值之前检查对象/模型的数组

Mongodb, checking the array of a object/ model before inserting new values

本文关键字:对象 模型 数组 检查 插入 新值之 Mongodb      更新时间:2023-09-26

我正在使用以下函数为用户注册"出价"。用户有一个bids数组,它将把bid放入数组中,并将其保存为用户模型的一部分。我使用以下功能来实现这一点:

 // Register client for bid
    module.exports.register = function(info, callback) {
    client_username = info['client_username'];
    bid_id = info['bid_id'];
    bid_title = info['bid_title'];
var query = {username: client_username};
    Client.findOneAndUpdate(
  query,
  {$addToSet: {"bids": {bid_id: bid_id, bid_title: bid_title}}},
  {safe: true, upsert: true},
  callback
    );
}

问题是,我没有检查客户是否已经在他们的出价数组中有这个出价。在使用$addToSet并将新的出价添加到数组之前,我对mongoDb和js的了解还不够好,无法检查出价数组。然而,我认为这就是$addToSet应该做的。

有人知道我如何迭代用户的出价数组,以检查我推送到数组中的出价是否已经存在于数组中吗?请参阅下面的整个投标模型和客户模型。。

 var mongoose = require('mongoose');
 // client Schema
 var clientSchema = mongoose.Schema({
    first_name: {
    type: String
    },
    last_name: {
        type: String
    },
    address: [{
        street_address:{type: String},
        city:{type: String},
        state:{type: String},
        zip:{type: String}
    }],
    username: {
        type: String
    },
    email: {
        type: String
    },
    bids:[{
        bid_id:{type: [mongoose.Schema.Types.ObjectId]},
        bid_title: {type:String}
    }]
    });
   var Client = module.exports = mongoose.model('Client', clientSchema);
 module.exports.getClientByUsername = function(username, callback){
     var query = {username: username};
    Client.findOne(query, callback);
 }
  // Register client for bid
 module.exports.register = function(info, callback) {
 client_username = info['client_username'];
 bid_id = info['bid_id'];
 bid_title = info['bid_title'];
var query = {username: client_username};
Client.findOneAndUpdate(
  query,
  {$addToSet: {"bids": {bid_id: bid_id, bid_title: bid_title}}},
  {safe: true, upsert: true},
  callback
  );
}

我在MongoDB实例上进行了本地测试。在使用FindAndModify()时,$AddtoSet仅在数组中不存在新出价的情况下才添加新出价。如果它在场,它什么也不做。这是我使用的查询:

db.collection.findAndModify({
    query: {
        "username": "ag102"
    },
    update: {
        $addToSet: {
            "bids": {
                "bid_id": 6,
                "bid_title": "ebay6"
            }
        }
    },
    upsert: true
});

您可以在模型中添加一个方法,这里有一个粗略的切口:

 clientSchema.methods.addBid = function (bid, cb) {
    /* Check the array here */
    for (var i = 0; i < this.bids.length; i++){
        if (this.bids[i].title == bid.bid_title) {
            /* do something */
            var error = true;
            cb("Bid already exists",this);
        }
    }
    if(!error){
        this.bids.push({
           "bid_id" : bid.bid_id,
           "bid_title" : bid.bid_title
        });
        cb(undefined,this);
    });
};

那么你会这样调用它:

var query = {username: client_username};
Client.findOne(query, function(err, client){
    client.addBid({bid_id: bid_id, bid_title: bid_title}, function(err, client){
        if(err) {
            /* do something */
        }else{
            client.save(function(err,client){
                if(err){
                   /* do something */
                }else{
                   /* do something */
                }
            });
         }
     });
});

我认为您想要使用$elemMatch关键字:https://docs.mongodb.org/v3.0/reference/operator/query/elemMatch/

为了提高效率,并假设您可以控制模式-我认为您应该将上面的内容更改为具有哈希键的对象,然后对出价id进行点表示查询。