我知道标题令人困惑,但让我更清楚地解释一下。
这是我的猫鼬模式的样子:
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();
});