1

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.

1 Answers1

0

edit: are you sure you're working with mongodb server 3.2 or higher supporting updateOne command? If not, use the .update() command rather than .updateOne()

 db.morosos.updateOne(
  { "idlor" : "XXX" },
  { $push: { "datos" : { "fecha": "XXXXX",
        "dato": "XXXX" } } }   )

should work but your update query { "idlor" : "XXX" } doesn't match the sample document.

I changed idlor to match the sample doc (XXX to XXXXX)

>  db.morosos.updateOne(
...   { "idlor" : "XXXXX" },
...   { $push: { "datos" : { "fecha": "XXXXX",
...         "dato": "XXXX" } } }   )

and the push worked just fine

> db.morosos.findOne()
{
    "_id" : ObjectId("58c9890a57c5133b63c05b86"),
    "idlor" : "XXXXX",
    "datos" : [
        {
            "fecha" : "XXXXXX",
            "dato" : "XXXXXX"
        },
        {
            "fecha" : "XXXXX",
            "dato" : "XXXX"
        }
    ],
}
bauman.space
  • 1,993
  • 13
  • 15
  • Thanks for try help but bro, XXX is just and example to don't write the information. Use X as a variable, think here XXX is same than XXXXX. – Miguel Sánchez Guerras Mar 15 '17 at 19:15
  • you're sure that mongodb server 3.2 or above which has the updateone command? The exact command seems to work on my 3.2 on ubuntu – bauman.space Mar 15 '17 at 19:17
  • I'm using mLab free server. mongod version: 3.2.11 (MMAPv1) – Miguel Sánchez Guerras Mar 15 '17 at 19:18
  • 2017-03-15T20:24:29.727+0100 E QUERY [thread1] WriteError: The field 'datos' must be an array but is of type Object in document {_id: ObjectId('58c81ba0fb02f3066e2af169')} : WriteError({ "index" : 0, "code" : 16837, "errmsg" : "The field 'datos' must be an array but is of type Object in document {_id: ObjectId('58c81ba0fb02f3066e2af169')}", "op" : { "q" : { "idlor" : "LOR01/16" }, "u" : { "$push" : { "datos" : { – Miguel Sánchez Guerras Mar 15 '17 at 19:36
  • "fecha" : "27/07/2016", "dato" : "Carta enviada" } } }, "multi" : false, "upsert" : false } }) WriteError@src/mongo/shell/bulk_api.js:469:48 Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:836:49 Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13 Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21 DBCollection.prototype.updateOne@src/mongo/shell/crud_api.js:550:17 @(shell):1:1 – Miguel Sánchez Guerras Mar 15 '17 at 19:36
  • the error is telling you that if you were to do a find on { "idlor" : "LOR01/16" }, datos is not a list. for the sake of argument, can you put the result of findOne({ "idlor" : "LOR01/16" }) in your question. – bauman.space Mar 15 '17 at 20:14
  • { "_id" : ObjectId("58c81ba0fb02f3066e2af169"), "idlor" : "LOR01/16", "comunidad" : "x", "vivienda" : "x", "demandado" : "x", "importe" : "x", "datos" : { "fecha" : "08/03/2017", "dato" : "Pagado Ejecucion" }, "date" : ISODate("1970-01-01T00:00:00Z") } – Miguel Sánchez Guerras Mar 15 '17 at 22:14
  • right, in your case, you can only $push if that "datos" field is already an array. You'd have to download the document, convert the field to a list, and update the whole document rather than issuing a push command. Here is a question that provides server side script that will go through and convert http://stackoverflow.com/questions/7401394/mongodb-type-change-to-array. – bauman.space Mar 16 '17 at 02:40
  • Thanks I add [] on "datos" [{}] on mLab "records/page" that allow you modify and it works now :) – Miguel Sánchez Guerras Mar 16 '17 at 09:00