These are chat archives for Automattic/mongoose

5th
May 2018
Daniel Netzer
@DanielNetzer
May 05 2018 06:45
So i've been tinkering with $pull operator
i'm iterating over all documents and removing an object from an array if it suits certain conditions but it seems to only remove it from the first document and not from the others
nvm, was missing { multi: true }
Ghost
@ghost~5928d90bd73408ce4f629b9e
May 05 2018 09:50

Is it possible to push new entries on a specific document's sub document?

const UserCommentSchema = new Schema({comment_from: 'string', comment_text: 'string'});
const ProductSchema = new Schema({
    product_img: {
        type: Array,
        required: true
    },
    product_description: {
        type: String,
    },
    product_by: {
        type: Schema.Types.ObjectId, ref: 'Account',
        required: true
    },
    post_date: {
        type: Date, 
        default: Date.now 
    },
    product_comments: [{UserCommentSchema}]
});

I want to add a user comment on the product but on mongoose documentation it doesn't tell how to add it to a specific document that is saved

http://mongoosejs.com/docs/subdocs.html

I want to push new entry to product_comments like this
[{comment_from: 'Bob', comment_text: 'Review'}, {comment_from: 'John', comment_text: 'Another review'}]
Kev
@lineus
May 05 2018 10:03
@2HexGFX_twitter is ProductSchema embedded in another schema or is that your model schema?
Ghost
@ghost~5928d90bd73408ce4f629b9e
May 05 2018 10:05
As i understood in the documentation i wrote them int the same file. UserProducts.js which is my model schema for User Products
Kev
@lineus
May 05 2018 10:07
either way, you can use the $push update operator with one of the update methods docs here, or you can find the doc with one of the find methods and use the mongoose push method.
something like Product.findOneAndUpdate({ _id: somedoc._id }, { $push: { product_comments: {comment_from: 'Bob', comment_text: 'Review'} } } )
or
let doc = await Product.findOne({ _id: somedoc._id})
doc.product_comments.push({comment_from: 'Bob', comment_text: 'Review'})
doc.save()
Ghost
@ghost~5928d90bd73408ce4f629b9e
May 05 2018 10:12

Ohh do i need to find the product id? and then push directly on the comments? I thought i could do something like this

  const comment = new Product({product_comments: 
       {
            comment_from: req.body.id,
            comment_text: req.body.comment
        }
});

That's why i'm confuse how can i save this on the specific product.

Kev
@lineus
May 05 2018 10:16
if someone is commenting on a specific product that exists already, then you'll need to have that product's id in order to add a comment to that product.
Ghost
@ghost~5928d90bd73408ce4f629b9e
May 05 2018 10:51

Hmm it doesn't get added to the database but the data from my response shows that the entries was push to product_comments

        Product.findOne({ _id: req.body.prod_id})
        .exec((err, prod) => {
            if(err){
                throw err;
            }

            if(prod){
                prod.product_comments.push({comment_from: 'Bob', comment_text: 'test'})
                res.send({message: 'Ok', data: prod})
            }

        })

here's how i initialize the product when saving it, is this a problem because im setting product_comments to empty array?

const prod = new Product({
                         product_img: images,
                         product_description: req.body.description,
                         product_by: req.body.id,
                         product_comments: []
                     });
                  // Save data
                    prod.save(callback)
Kev
@lineus
May 05 2018 10:52
you have to call prod.save() after you push the comment.
Ghost
@ghost~5928d90bd73408ce4f629b9e
May 05 2018 10:54
@lineus it's working now, thank you super big help
Kev
@lineus
May 05 2018 10:54
anytime :)