当尝试在Mongodb和nodejs中建立$查找关系时出现错误

问题描述 投票:0回答:1

我还处于学习阶段。

我在Mongodb中建立关系时遇到麻烦。

找不到错误404我用两个表中的数据以及接受的培训对集合中的所有操作进行了操作。但是以某种方式我无法成功运行它。

模型书籍

    const Book = require('../Models/Book');

    router.post('/new', function(req, res, next) {
        const  book = new Book({
            title: 'Hikayelerde ve çocuk',
            userId: '5ed267005e5d568b58cd17f7',
            published: true,
            category: 'Hikaye',
            comment: [
                {
                    author: "Macit",
                    mail: "[email protected]",
                    subject:"Kitap fena değil",
                    message: "Bu kitap oldukça güzel ben çok beğendim ",
                },
                {
                    author: "Mutlu",
                    mail: "[email protected]",
                    subject:"Kitap fena değil",
                    message: "Bu kitap oldukça güzel ben çok beğendim ",

                },
            ],
            meta:
                {
                    votes: 3,
                    favs: 3
                },


        });

        book.save((err, data) => {

            if (err)
                console.log(err);

            res.json(data);
        });
    });
// aggregate $lookup
router.get('aggregate-lookup', (req, res) => {
    Book.aggregate([
        {
            $lookup: {
                from: 'users',
                localField: 'userId',
                foreignField: '_id',
                as: 'user'
            }
        }
    ], (err, result) => {
        res.json(result);
    });
});

module.exports =路由器;

图书收藏

/* 1 */
{
    "_id" : ObjectId("5ed26a1ca997a08c2d6d59bf"),
    "published" : true,
    "title" : "Hikayelerde ve çocuk",
    "userId" : ObjectId("5ed267005e5d568b58cd17f7"),
    "category" : "Hikaye",
    "comment" : [ 
        {
            "_id" : ObjectId("5ed26a1ca997a08c2d6d59c0"),
            "author" : "Macit",
            "mail" : "[email protected]",
            "message" : "Bu kitap oldukça güzel ben çok beğendim "
        }, 
        {
            "_id" : ObjectId("5ed26a1ca997a08c2d6d59c1"),
            "author" : "Mutlu",
            "mail" : "[email protected]",
            "message" : "Bu kitap oldukça güzel ben çok beğendim "
        }
    ],
    "meta" : {
        "votes" : 3,
        "favs" : 3
    },
    "publishedAt" : ISODate("2020-05-30T14:13:48.440Z"),
    "__v" : 0
}

书籍

const mongoose = require('mongoose');
const Schema = mongoose.Schema;


const BookSchema = new Schema({

    title: String,

    comment: [
        {
            author: String,
            mail: String,
            subjet: String,
            message: String,
        },
    ],
    category: String,
    meta: {
        votes: Number,
        favs: Number
    },


    published: {
        type: Boolean,
        default: true
    },

    publishedAt : {
        type: Date,
        // şuanki tarih
        default: Date.now
    },
    userId: {
        type: mongoose.Schema.Types.ObjectID
    },

});
module.exports = mongoose.model('Book', BookSchema);

用户集合

/* 1 */
{
    "_id" : ObjectId("5ed267005e5d568b58cd17f7"),
    "published" : true,
    "name" : "Macit Mutlu Sarı",
    "age" : "34",
    "about" : "Ankara doğumlu ve güzel sanatlar mezunu 18 yıllık deneyimi olan bir profesyonel",
    "publishedAt" : ISODate("2020-05-30T14:00:32.322Z"),
    "__v" : 0
}
node.js mongodb lookup
1个回答
0
投票

用_id替换_Id

$lookup: {
               from: 'users',
               localField: '_id', //Replace here
               foreignField: 'userId',
               as: 'user'
           }
© www.soinside.com 2019 - 2024. All rights reserved.