I'm just starting learning mongodb and starting personal app for own business.
I'll explain here my problem because I can't solve it myself and I try many ways.
I create one object like this:
db.morosos.insert({
"idlor": "XXXXX",
"comunidad": "XXXXX",
"vivienda": "XXXXX",
"demandado": "XXXXX",
"importe": "XXXXX",
"datos": [
{
"fecha": "XXXXXX",
"dato": "XXXXXX"
}],
"date": new Date("<dd-mm-YYYY>")})
Then I want to add new "datos" with new "fecha" and new "dato" like an array.
I try to use:
db.morosos.updateOne(
{ "idlor" : "XXXXX" },
{ $inc: { "datos" : { "fecha": "XXXXX", "dato": "XXXX" } } } )
db.morosos.updateOne(
{ "idlor" : "XXX" },
{ $push: { "datos" : { "fecha": "XXXXX",
"dato": "XXXX" } } } )
db.morosos.update(
{ "idlor" : "XXXX" },
{
$addToSet: {
"datos": {
"fecha": "XXXXX",
"dato": "XXXXX"
}
}
} )
And no ones works like I want, it just change the information or throw me a fail like:
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "Cannot apply $addToSet to a non-array field. Field named 'datos' has a non-array type Object in the document _id: ObjectId('58c81ba0fb02f3066e2af169')"
}})
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "The field 'datos' must be an array but is of type Object in document {_id: ObjectId('58c81ba0fb02f3066e2af169')}"
}})
So at the moment I don't know what to do, I'm still searching how to solve it.
Thanks.