Node.js 可以将Mongo数据保存到多个文档中
我的问题是,是否可以将Mongo数据保存到多个文档中并将它们链接在一起?我有非常大的json文件,所以我想把它们分割成多个文件,并以这种方式保存它们,每个文档文件的大小会更小吗 这是我的代码,它创建了非常大的JSON文件和子数组,我希望使每个文档的大小尽可能小Node.js 可以将Mongo数据保存到多个文档中,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我的问题是,是否可以将Mongo数据保存到多个文档中并将它们链接在一起?我有非常大的json文件,所以我想把它们分割成多个文件,并以这种方式保存它们,每个文档文件的大小会更小吗 这是我的代码,它创建了非常大的JSON文件和子数组,我希望使每个文档的大小尽可能小 router.post("/request/partial", async (req, res) => { const partial = new PartialRequest({ startingDa
router.post("/request/partial", async (req, res) => {
const partial = new PartialRequest({
startingDate: req.body.startingDate,
requestID: req.body.requestID,
whoCreated: req.body.whoCreated,
isNewRequest: true,
specialRequest: req.body.specialRequest,
contactInformation: req.body.contactInformation,
requestInformation: req.body.requestInformation,
orderInformation: req.body.orderInformation,
jobSiteInformation: req.body.jobSiteInformation,
installations : req.body.installations;
});
await partial.save().then(result => {
return res.status(200).json({
message: 'Handling POST request to /partial',
result: result
});
});
await Request.findOneAndRemove({
requestID: req.body.requestID
}).then(data => {
return res.status(200).json(data);
}).catch(error => {
return res.status(500).json({
error: error
})
})
})
在这里,您可以更新for循环中的文档 在我的示例中,根据数据库更改必要的关键字
userController.signUp = async function(req,res){
req.body.installation = [
{
"phase":"abc",
"phase" : "driveOne",
"text" : "Drive One",
"textKey" : "",
"index" : "1.0",
"subjects":[{
"id":"1.1.1",
"text":"test",
"textKey":"",
}]
},
{
"phase":"abc",
"phase" : "driveOne",
"text" : "Drive One",
"textKey" : "",
"index" : "1.0",
"subjects":[{
"id":"1.1.1",
"text":"test",
"textKey":"",
}]
},
{
"phase":"abc",
"phase" : "driveOne",
"text" : "Drive One",
"textKey" : "",
"index" : "1.0",
"subjects":[{
"id":"1.1.1",
"text":"test",
"textKey":"",
}]
},
{
"phase":"abc",
"phase" : "driveOne",
"text" : "Drive One",
"textKey" : "",
"index" : "1.0",
"subjects":[{
"id":"1.1.1",
"text":"test",
"textKey":"",
}]
},
]
//Your DB name instead of userModel
userModel.findOne({_id: "5d1d9691019db61515450574"})
.exec(async (err , found)=>{
if(err){
res.send(err);
}else{
for(var i = 0; i< req.body.installation.length; i++){
var hey = req.body.installation[i];
found.installation[i] = req.body.installation[i];
console.log("=============================+>" ,found.installation)
await userModel.findOneAndUpdate({_id: found._id} , found, {upsert: true , new: true});
}
res.send(found);
}
});
}
userController.signUp=异步函数(req,res){
req.body.installation=[
{
“阶段”:“abc”,
“阶段”:“驱动一号”,
“文本”:“驾驶一号”,
“文本键”:“,
“索引”:“1.0”,
“主题”:[{
“id”:“1.1.1”,
“文本”:“测试”,
“文本键”:“,
}]
},
{
“阶段”:“abc”,
“阶段”:“驱动一号”,
“文本”:“驾驶一号”,
“文本键”:“,
“索引”:“1.0”,
“主题”:[{
“id”:“1.1.1”,
“文本”:“测试”,
“文本键”:“,
}]
},
{
“阶段”:“abc”,
“阶段”:“驱动一号”,
“文本”:“驾驶一号”,
“文本键”:“,
“索引”:“1.0”,
“主题”:[{
“id”:“1.1.1”,
“文本”:“测试”,
“文本键”:“,
}]
},
{
“阶段”:“abc”,
“阶段”:“驱动一号”,
“文本”:“驾驶一号”,
“文本键”:“,
“索引”:“1.0”,
“主题”:[{
“id”:“1.1.1”,
“文本”:“测试”,
“文本键”:“,
}]
},
]
//您的数据库名而不是userModel
findOne({u id:“5d1d9691019db61515450574”})
.exec(异步(错误,找到)=>{
如果(错误){
res.send(err);
}否则{
对于(变量i=0;i”,已找到。安装)
等待userModel.findOneAndUpdate({u-id:found.\u-id},found,{upsert:true,new:true});
}
res.send(找到);
}
});
}
我尝试过使用20万条记录,但没有给出错误。您希望所有数据都存储在单个
模型中,但要逐步存储。对吗?可能是的,更好的解决方案是什么?我应该拆分模型吗?我只想在每个mongo文档中存储尽可能小的数据。您可以在多个文档中的单个集合中存储数据。这样会更好。您好,我已经修改了部分代码,以便您能够理解。。。由于我有安装阵列,但当我运行此函数时,它根本不存储安装,并且在文档安装:[]中只存储空阵列更新。对不起,我犯了那个错误@MuhammadAlinot正是我想要的。。。安装是一个阵列,可以非常大。。。因此,它必须以某种方式序列化或分块存储在数据库中。将其存储到一个文档中会使其太大。不过,谢谢您的帮助。但我需要某种方法来存储到多个文件中,这样它就不会创建一个大文档..好的,让我们用新的解决方案返回给您@阿里