“Mongoose rejoigne plusieurs collections” Réponses codées

Mongoose rejoigne plusieurs collections

    var mongoose = require('mongoose');
    var userCollection = require('./user');//import user model file
    var resources = {
    nick_name: "$nick_name",
    email: "$email"};

    userCollection.aggregate([{
            $group: resources
        }, {
            $lookup: {
                from: "Comments", // collection to join
                localField: "_id",//field from the input documents
                foreignField: "user_id",//field from the documents of the "from" collection
                as: "comments"// output array field
            }
        }, {
            $lookup: {
                from: "Post", // from collection name
                localField: "_id",
                foreignField: "user_id",
                as: "posts"
            }
        }],function (error, data) {
         return res.json(data);
     //handle error case also
});
Gifted Gazelle

Comment obtenir des données à partir de plusieurs tables Mongoose

const userSchema = new Schema({  
    nick_name:{type:String},  
    email: {  
        type: String,  
        trim: true,  
        required: '{PATH} is required!',
        index: true,
    },
    comments: [{ type: Schema.Types.ObjectId, ref:'Comment' }],
    posts: [{ type: Schema.Types.ObjectId, ref:'Post' }]
}, {timestamps: true});

mongoose.model('User', userSchema);
Famous Fish

Réponses similaires à “Mongoose rejoigne plusieurs collections”

Questions similaires à “Mongoose rejoigne plusieurs collections”

Plus de réponses similaires à “Mongoose rejoigne plusieurs collections” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code