krlosruiz
krlosruiz

Reputation: 113

Unable to add objects into an array in mongodb using mongoose?

I'm creating an aplication with nodejs and mongodb and I have this:

var mongoose = require('mongoose');
var db = mongoose.connect("mongodb://localhost/dbTienda");

var esquemaUsuarios = new mongoose.Schema({
nombre: String,
apellido: String,
email: String,
contrasena: String,
notificaciones: Number,
notificacionesLista: [String],
articulos_vendidos: Number,
productos: [{
    nombre: String,
    imgPath: String,
    precio: Number,
    ciudades: [String],
    tags:  [String],
    descripcion: String
}]
}), usuarios = db.model("Usuarios",esquemaUsuarios);

The problem is that I can't add anything in productos, the idea I have is to make some like this

productos: {
{nombre:XXXXX,imgepath:XXXXX,precio:XXXXX,ciudades:{ XXXXX },tags:{ XXXXX },descripcion: XXXXX},
{nombre:XXXXX,imgepath:XXXXX,precio:XXXXX,ciudades:{ XXXXX },tags:{ XXXXX },descripcion: XXXXX},
{nombre:XXXXX,imgepath:XXXXX,precio:XXXXX,ciudades:{ XXXXX },tags:{ XXXXX },descripcion: XXXXX},
{nombre:XXXXX,imgepath:XXXXX,precio:XXXXX,ciudades:{ XXXXX },tags:{ XXXXX },descripcion: XXXXX}
}

I want to add many objects for example first object is maybe a celphone second a tv ... etc. I can't add anything and I'm using this:

mongoose.model('Usuarios').update({
productos:[{$pushAll:{
    nombre: informacion.pNombre,
    imgPath: informacion.pImagen,
    precio: informacion.pPrecio,
    ciudades: informacion.pCiudades,
    tags: informacion.pTags,
    descripcion: informacion.pDescripcion
}},{upsert:true}]
});

but it doesn't work! I can edit nombre apellido email ... etc but I can't with productos.

Upvotes: 1

Views: 829

Answers (2)

bFunc
bFunc

Reputation: 1455

imho you need to define separate schema for your 'productos', and store them by reference inside 'esquemaUsuarios', it will give you more flexibility and save db storage when you need to share the same product

Upvotes: 0

Neil Lunn
Neil Lunn

Reputation: 151102

The problem here is with the usage of the .update() method and the actual operator you want to use is $push.

The update method takes it's first argument as a query to "find" the document(s) that you want to update, and the second argument is the actual update statement. So in your case:

mongoose.model('Usuarios').update(
    { /* query to match document(s) */ },
    {
        "$push": {
            nombre: informacion.pNombre,
            imgPath: informacion.pImagen,
            precio: informacion.pPrecio,
            ciudades: informacion.pCiudades,
            tags: informacion.pTags,
            descripcion: informacion.pDescripcion
        }
    },
    { upsert:true },
    function(err,numAffected) {

    }
);

The "options" you have set for upsert will create a "new" document if your "query" condition does not actually match any document in your collection.

If your intention is not to update multiple documents the perhaps your usage might be better suited to the .findOneAndUpdate() or .findByIdAndUpdate() method instead.

Just for notes, the $pushAll operator has actually been deprecated in recent releases of MongoDB and the preferred way is to use the $each modifier in combination with $push. This is because this functionality is also shared with the $addToSet operator in the same way. The intended usage is for adding "multiple" array items at once.

Upvotes: 2

Related Questions