0

我知道标题令人困惑,但让我更清楚地解释一下。

这是我的猫鼬模式的样子:

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.

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

对象一:

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

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

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

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

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();
});
4

1 回答 1

1

要添加预创建模式的方法,您可以schema.queue按照此讨论来完成,这里是下面的测试代码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 }

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

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');
        })  
    }
})

保存结果如上,为确保您的海量数据被一一保存,您可以使用async.series.

于 2016-03-26T01:27:24.933 回答