Node.js 如何检查MongoDB以查看集合中是否已存在项
我已将我的项目作为对象数组放入数据库中的集合中。现在我注意到每次服务器重新启动时都会添加这些项,结果是6个产品而不是3个产品。如何检查数据库以查看集合中是否已存在项,从而使其不重复?目前我得到的是“ReferenceError:标题未定义” Product.js(模型) js(填充产品集合)Node.js 如何检查MongoDB以查看集合中是否已存在项,node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,我已将我的项目作为对象数组放入数据库中的集合中。现在我注意到每次服务器重新启动时都会添加这些项,结果是6个产品而不是3个产品。如何检查数据库以查看集合中是否已存在项,从而使其不重复?目前我得到的是“ReferenceError:标题未定义” Product.js(模型) js(填充产品集合) module.exports=Product=mongoose.model('products',ProductSchema); const Product=require(“../models/Produ
module.exports=Product=mongoose.model('products',ProductSchema);
const Product=require(“../models/Product”);
const mongoose=require('mongoose');
常数乘积=[
新产品({
imagePath:“../client/src/Components/Layout/Media/armadillo.png”,
标题:“穿山甲”,
描述:“这是一只穿山甲”,
价格:15000
}),
新产品({
imagePath:“../client/src/Components/Layout/Media/croc.png”,
标题:“古巴科科迪亚”,
描述:“这是一只鳄鱼”,
价格:15000
}),
新产品({
imagePath:“../client/src/Components/Layout/Media/monkey.png”,
标题:“金色长臂猿”,
描述:“这是一只猴子”,
价格:15000
})
];
for(设i=0;i{
如果(!产品){
产品[i].save();
}
});
}
mongoose.disconnect();
您在查询中犯了两个错误
使用产品[i].title
而不是title
使用结果
或任何其他变量更改产品
for(设i=0;i{//使用结果更新产品
console.log(结果)
如果(!结果){
产品[i].save();
}
});
}
});
您在查询中犯了两个错误
使用产品[i].title
而不是title
使用结果
或任何其他变量更改产品
for(设i=0;i{//使用结果更新产品
console.log(结果)
如果(!结果){
产品[i].save();
}
});
}
});
还有另一种方法可以做到这一点;使用原始对象数组,而不是带有的Mongoose文档
允许您在一个命令中向MongoDB服务器发送多个insertOne
、updateOne
、updateMany
、replaceOne
、deleteOne
和/或deleteMany
操作,而不是使用for循环向服务器发送多个命令的当前设置。在这种情况下,您需要多个操作,更新操作为
例如,假设您有一个数组
const products = [
{
imagePath: '../client/src/Components/Layout/Media/armadillo.png',
title: 'Pangolin',
description: "This is a pangolin",
price: 15000
},
{
imagePath: '../client/src/Components/Layout/Media/croc.png',
title: 'Cuban corcodile',
description: "This is a croc",
price: 15000
},
{
imagePath: '../client/src/Components/Layout/Media/monkey.png',
title: 'Golden Gibbon',
description: "This is a monkey",
price: 15000
}
];
您可以设置操作列表,其形式如下
[
{ updateOne :
{
"filter" : <document>,
"update" : <document>,
"upsert" : <boolean>
}
}
]
然后,您可以在上面应用,如下所示:
Product.bulkWrite(ops).then(result => console.log(result))
在上述情况下,如果存在匹配的文档,则使用进行的更新不会执行任何操作,因为更新操作不会导致插入,因为找到了与查询匹配的文档
如果使用原始实现,则需要修复查询,然后在for循环中建立承诺,如下所示:
let promises = [];
for (let i = 0; i < products.length; i++) {
const productPromise = Product.findOne({
title: products[i].title // <- fixed
}).then(product => {
if (!product) { // <-- fixed
return products[i].save();
}
});
promises.push(productPromise);
}
Promise.all(promises).then(result => console.log(result));
let promissions=[];
for(设i=0;i
还有另一种方法可以做到这一点:使用原始对象数组,而不是使用的Mongoose文档
允许您在一个命令中向MongoDB服务器发送多个insertOne
、updateOne
、updateMany
、replaceOne
、deleteOne
和/或deleteMany
操作,而不是当前设置中使用for loo向服务器发送多个命令p、 在这种情况下,您需要多个操作,更新操作为
例如,假设您有一个数组
const products = [
{
imagePath: '../client/src/Components/Layout/Media/armadillo.png',
title: 'Pangolin',
description: "This is a pangolin",
price: 15000
},
{
imagePath: '../client/src/Components/Layout/Media/croc.png',
title: 'Cuban corcodile',
description: "This is a croc",
price: 15000
},
{
imagePath: '../client/src/Components/Layout/Media/monkey.png',
title: 'Golden Gibbon',
description: "This is a monkey",
price: 15000
}
];
您可以设置操作列表,其形式如下
[
{ updateOne :
{
"filter" : <document>,
"update" : <document>,
"upsert" : <boolean>
}
}
]
然后,您可以在上面应用,如下所示:
Product.bulkWrite(ops).then(result => console.log(result))
在上述情况下,如果存在匹配的文档,则使用进行的更新不会执行任何操作,因为更新操作不会导致插入,因为找到了与查询匹配的文档
如果使用原始实现,则需要修复查询,然后在for循环中建立承诺,如下所示:
let promises = [];
for (let i = 0; i < products.length; i++) {
const productPromise = Product.findOne({
title: products[i].title // <- fixed
}).then(product => {
if (!product) { // <-- fixed
return products[i].save();
}
});
promises.push(productPromise);
}
Promise.all(promises).then(result => console.log(result));
let promissions=[];
for(设i=0;i
Product.findOne({title:title})中未定义获取引用错误bcuz title。应将title:title
更改为title:products[i]。title
在Product.findOne()中未定义获取引用错误bcuz title。您应该将标题:title
更改为标题:产品[i]。Product.findOne()中的标题