[英]Can't add custom object into mongoDB Document Array
I'm trying to add a new object to my mongodb document inside an array. 我试图在数组中的mongodb文档中添加一个新对象。
I have a NodeJS project using MongoDB which has a collection called "Teste" where i'm saving some random data. 我有一个使用MongoDB的NodeJS项目,该项目有一个名为“ Teste”的集合,我在其中保存了一些随机数据。
Amongst that data is an array called "ArrayTeste". 在这些数据中有一个叫做“ ArrayTeste”的数组。 Currently it is only saving multiple strings because I named my inputs the same thing, so it automatically does it for me. 目前,它只保存多个字符串,因为我将输入命名为相同的东西,因此它会自动为我执行此操作。
But I don't want to save each element as an individual string, i need to get these informations, group them in an object and then add it to the array. 但是我不想将每个元素另存为单独的字符串,我需要获取这些信息,将它们分组在一个对象中,然后将其添加到数组中。
Here is my code snippet in NodeJS: 这是我在NodeJS中的代码片段:
ServicosModel.prototype.Teste = function (req, res) {
console.log("Metodo Teste");
var query =
{
$push:
{
ArrayTeste:
{
Dado1: req.body.Dado1,
Dado2: req.body.Dado2
}
}
}
console.log(query)
this._connection.open(function (errConn, mongoClient) {
console.log("Entrou open")
if (errConn) {
res.end("Deu erro" + errConn);
}
mongoClient.collection("teste", function (errColl, collection) {
if (errColl) {
res.end("Deu erro" + errColl);
}
console.log("Entrou collection")
collection.update(query, function (errUpdate, result) {
console.log("Entrou update")
if (errUpdate) {
res.end("Deu erro" + errUpdate);
} else {
res.end("Deu certo " + result);
}
});
});
});
}
And here is the mongoDB document structure: 这是mongoDB文档结构:
{
"_id" : ObjectId("595bf19febbf3c14e481bc28"),
"id" : "2",
"Titulo" : "TItulo do negocio",
"ArrayTeste" : [
"dado1",
"dado2"
]
} }
The "id" parameter is one created by me to easy the $elemMatch used in previous tests, so I don't have to search for the _id of the document. “ id”参数是我为简化先前测试中使用的$ elemMatch而创建的参数,因此我不必搜索文档的_id。
When I run the code and insert stuff into the inputs, I am presented with this error: 当我运行代码并将内容插入输入中时,出现此错误:
(node:8712) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: document must be a valid JavaScript object (节点:8712)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):MongoError:文档必须是有效的JavaScript对象
and I have absolutely no idea of what is happening. 而且我完全不知道发生了什么。 the application simply freezes. 该应用程序只是冻结。 I have searched through the posts and tried some stuff with $set and $addToSet, but the same error persists. 我已经搜索了帖子,并尝试了一些使用$ set和$ addToSet的东西,但是仍然存在相同的错误。
Any help is appreciated, thanks in advance! 任何帮助表示赞赏,在此先感谢!
To update a document you need two mandatory parameters: 要更新文档,您需要两个强制性参数:
criteria
- to select documents to update criteria
-选择要更新的文档 update
- to modify selected documents update
-修改选定的文档 Here is the driver documentation: https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html#update . 这是驱动程序文档: https : //mongodb.github.io/node-mongodb-native/markdown-docs/insert.html#update 。
The error says that collection.update
expects an object (the second parameter) and it is receiving a function (your callback function). 该错误表明collection.update
需要一个对象(第二个参数),并且正在接收一个函数(您的回调函数)。
To get your code working: 要使代码正常工作:
var select = {id: '2'}; // Here we are choosing the document
collection.update(select, query, function (errUpdate, result) {
if (errUpdate) {
res.end("Deu erro" + errUpdate);
} else {
res.end("Deu certo " + result);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.