1

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Omg thank you so much I was really stressed with this problem
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.