U
U
UNy2018-06-09 02:04:25
JavaScript
UNy, 2018-06-09 02:04:25

Mongodb ref populate?

How to work with links in Mongo? I have a user:

let alex = new User({
    name: "Alex",
    age:21
});
alex.save();

and there are comments the diagram looks like this:
let PostSchema = new mongoose.Schema({
    postedBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    comments: [{
        text: String,
    }]
});

How can I make comments from one user be pushed into an array, rather than creating a new document?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
last713, 2018-06-14
@last713

Операторы работы с массивом $push, $addToSet итд.

Post.updateOne({
    postedBy: userId,
}, {
    $push: {
        comments: text,
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question