猫鼬需要创建对象,但是如果另一个对象已经具有该属性,则跳过该属性 [英] Mongoose needs to create objects, but skip the property if another object already has this property

查看:65
本文介绍了猫鼬需要创建对象,但是如果另一个对象已经具有该属性,则跳过该属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道标题很混乱,但让我更清楚地解释.

I know the title is confusing but let me explain more clearly.

这是我的猫鼬图式:

var LocationSchema = new Schema({
  locationKey: {type: String, unique: true},
  wheat: Array,
  barley: Array,
  cty: {type: String, unique: true},
  twp: {type: String, index: { unique: true} , dropDups: true},
  rge: {type: String, unique: true},
});

我写了一些代码,将使用此架构创建3500个位置.问题在于,许多位置的twp值都相同.在这种情况下,如果已创建的任何其他对象具有相同的twp,则我仍然需要它来创建对象,而不创建twp.

I wrote some code that will create 3500 locations using this schema. The problem is that lots of locations have the same value for twp. When this is the case, I need it to still create the object, but not create the twp if any other object that has been created has the same twp.

正如您在上面看到的,我尝试使用上面的独特方法.为了更清楚,这是两个示例对象:

As you can see above I tried using the unique approach above. Here is two sample objects for more clarity:

对象一:

{
    "_id" : ObjectId("56f5af9547a341720b0b25cd"),
    "rge" : "034E",
    "twp" : "001N",
    "cty" : "003",
    "locationKey" : "003001N034E",
    "__v" : 0
}

如果我要创建一个具有twp: 001N的新对象,我希望创建它,但是看起来像这样:

If I am going to create a new object, that has twp: 001N, I would like it to be created, but to look like so:

{
    "_id" : ObjectId("56f5af9547a341720b0b25cd"),
    "rge" : "034W",
    "cty" : "004",
    "locationKey" : "003001N034E",
    "__v" : 0
}

在服务器JS中,我有一个3,000个对象的数组,我正在遍历该数组以为每个项目创建一个位置对象,如下所示:

In server JS I have an array of 3,000 objects, I am looping through this array to create a Location Object for each item like so:

locations.forEach(function(item){
  var location = new Location();
  location.locationKey = item.locationKey.trim();
  location.cty = item.cty.trim();
  location.twp = item.twp.trim();
  location.rge = item.rge.trim();
  location.wheat = item.wheat;
  location.barley = item.barley;
  location.save();
});

推荐答案

要在创建模式之前添加方法,您可以按照schema.queue进行操作/issues/672"rel =" nofollow>讨论,这是mongoose v4.4.6

To add methods pre creating schema, you could do it through schema.queue per this discussion, here are the test codes under mongoose v4.4.6

var twpSet = new Set();

LocationSchema.methods.twp1 = function () {
    var curTwp = this.twp;

    if (twpSet.has(curTwp)) {
        this.twp = undefined; // remove the twp field once duplicated
    } else {
        twpSet.add(curTwp); // save the existing twp value
    }
};

LocationSchema.queue('twp1'); 

测试数据

var l1 = new Loca({
    locationKey: 'k1',
    cty: 'c1',
    twp: 't1',
    rge: 'r1'
});

console.log(l1);
l1.save(function(err) {
    if (err)
        console.log(err);
    else 
        console.log('save location1 successfully');
})

var l2 = new Loca({
    locationKey: 'k2',
    cty: 'c2',
    twp: 't1',
    rge: 'r2'
});

console.log(l2);
l2.save(function(err) {
    if (err)
        console.log(err);
    else 
        console.log('save location2 successfully');
})    

结果是

{ "_id" : ObjectId("56f5e484a22885dd0362a39a"), "locationKey" : "k1", "cty" : "c1", "twp" : "t1", "rge" : "r1", "barley" : [ ], "wheat" : [ ], "__v" : 0 }
{ "_id" : ObjectId("56f5e484a22885dd0362a39b"), "locationKey" : "k2", "cty" : "c2", "rge" : "r2", "barley" : [ ], "wheat" : [ ], "__v" : 0 }


另一种选择是通过.pre('save'中间件在save新文档之前检查现有文档,如果找到重复的twp,则将其删除在新文档中,然后保存.


Another option is to check the existing document before save the new document through .pre('save' middleware, if find the duplicate twp, remove it in the new document, then save it.

LocationSchema.pre('save', function(next) {
    var curTwp = this.twp;
    console.log(curTwp);
    var obj = this;
    Loca.findOne({twp: curTwp}, function(err, loc) {
        if (err)
            next(err);
        else if (loc) {
            // find the duplicate twp before save
            obj.twp = undefined;
            next();
        }else 
            next();
    })

});

var Loca = mongoose.model('Loca', LocationSchema);

测试代码

var l1 = new Loca({
    locationKey: 'k1',
    cty: 'c1',
    twp: 't1',
    rge: 'r1'
});
var l2 = new Loca({
    locationKey: 'k2',
    cty: 'c2',
    twp: 't1',
    rge: 'r2'
});

console.log(l1);
l1.save(function(err) {
    if (err)
        console.log(err);
    else {
        l2.save(function(err) {
            if (err)
                console.log(err);
            else 
                console.log('save location2 successfully');
        })  
    }
})

按上述方式保存结果,以确保将大量数据一一保存,您可以使用

Save result as above, to make sure your huge data is saved one by one, you could use async.series.

这篇关于猫鼬需要创建对象,但是如果另一个对象已经具有该属性,则跳过该属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆