加入两个集合时,如何在两个集合之间使用一个_id

时间:2019-04-24 16:44:35

标签: node.js mongodb join mongoose mongoose-schema

我想联接两个表。但是在事件数组中应该有一个事件。但是有两个。

Word模式

const WordSchema = new Schema({
    word:{
        type:String,
    },
    translate: {
        type:String,
    },
    kind:{
        type:String,
    },
    exampleSentence: {
        type:String,     
    },
    isCorrect: {
        type:Boolean
    },
    event: {
        type:mongoose.Schema.ObjectId,
        required:true
    }
});

module.exports = mongoose.model("Word",WordSchema);

测试架构

const TestSchema = new Schema({
    status: {
        type:Schema.Types.ObjectId,
        required:true
    },
    createdAt: {
        type:Date,
        default:Date.now()
    },
    TenMinutesLater: {
        type:Date,
        default: new Date(+new Date() + 10*1000)
    },
    OneWeekLater: {
        type:Date,
        default: new Date(+new Date() + 7*24*60*60*1000) // 1 week later
    },
    OneMonthLater: {
        type:Date,
        default: new Date(+new Date() + 30*24*60*60*1000) // 1 month later
    },
    FourMonthLater: {
        type:Date,
        default: new Date(+new Date() + 122*24*60*60*1000) // 4 month later
    }

});

module.exports = mongoose.model('Test',TestSchema);

这些是我的记录

单词收集记录

{
        "_id" : ObjectId("5cc03a8381bafb40b884e0e6"),
        "word" : "Mouse",
        "translate" : "Fare",
        "kind" : "İsim",
        "exampleSentence" : "asdasasf",
        "__v" : 0
}
{
        "_id" : ObjectId("5cc03ae081bafb40b884e0e8"),
        "word" : "Keyboard",
        "translate" : "Klavye",
        "kind" : "İsim",
        "exampleSentence" : "asdbaksbdasd",
        "__v" : 0
}

测试收集记录

> db.tests.find().pretty();
{
        "_id" : ObjectId("5cc03ac581bafb40b884e0e7"),
        "createdAt" : ISODate("2019-04-24T10:28:54.207Z"),
        "TenMinutesLater" : ISODate("2019-04-24T10:29:04.207Z"),
        "OneWeekLater" : ISODate("2019-05-01T10:28:54.207Z"),
        "OneMonthLater" : ISODate("2019-05-24T10:28:54.207Z"),
        "FourMonthLater" : ISODate("2019-08-24T10:28:54.207Z"),
        "__v" : 0
}
{
        "_id" : ObjectId("5cc03ae881bafb40b884e0e9"),
        "createdAt" : ISODate("2019-04-24T10:28:54.207Z"),
        "TenMinutesLater" : ISODate("2019-04-24T10:29:04.207Z"),
        "OneWeekLater" : ISODate("2019-05-01T10:28:54.207Z"),
        "OneMonthLater" : ISODate("2019-05-24T10:28:54.207Z"),
        "FourMonthLater" : ISODate("2019-08-24T10:28:54.207Z"),
        "__v" : 0
}

我已经尝试过这段代码

router.get("/", (req,res) => {
    const promise = Word.aggregate([
        {
        $lookup:
           {
             from: 'tests',
             localField: 'event',
             foreignField: 'status',
             as: 'events'
           }
        ]);
        promise.then(data => {
            res.json(data);
        })
        .catch(err=>{throw err});
})

这是输出

[
    {
        "_id": "5cc03a8381bafb40b884e0e6",
        "word": "Mouse",
        "translate": "Fare",
        "kind": "İsim",
        "exampleSentence": "asdasasf",
        "__v": 0,
        "events": [
            {
                "_id": "5cc03ac581bafb40b884e0e7",
                "createdAt": "2019-04-24T10:28:54.207Z",
                "TenMinutesLater": "2019-04-24T10:29:04.207Z",
                "OneWeekLater": "2019-05-01T10:28:54.207Z",
                "OneMonthLater": "2019-05-24T10:28:54.207Z",
                "FourMonthLater": "2019-08-24T10:28:54.207Z",
                "__v": 0
            },
            {
                "_id": "5cc03ae881bafb40b884e0e9",
                "createdAt": "2019-04-24T10:28:54.207Z",
                "TenMinutesLater": "2019-04-24T10:29:04.207Z",
                "OneWeekLater": "2019-05-01T10:28:54.207Z",
                "OneMonthLater": "2019-05-24T10:28:54.207Z",
                "FourMonthLater": "2019-08-24T10:28:54.207Z",
                "__v": 0
            }
        ]
    },
    {
        "_id": "5cc03ae081bafb40b884e0e8",
        "word": "Keyboard",
        "translate": "Klavye",
        "kind": "İsim",
        "exampleSentence": "asdbaksbdasd",
        "__v": 0,
        "events": [
            {
                "_id": "5cc03ac581bafb40b884e0e7",
                "createdAt": "2019-04-24T10:28:54.207Z",
                "TenMinutesLater": "2019-04-24T10:29:04.207Z",
                "OneWeekLater": "2019-05-01T10:28:54.207Z",
                "OneMonthLater": "2019-05-24T10:28:54.207Z",
                "FourMonthLater": "2019-08-24T10:28:54.207Z",
                "__v": 0
            },
            {
                "_id": "5cc03ae881bafb40b884e0e9",
                "createdAt": "2019-04-24T10:28:54.207Z",
                "TenMinutesLater": "2019-04-24T10:29:04.207Z",
                "OneWeekLater": "2019-05-01T10:28:54.207Z",
                "OneMonthLater": "2019-05-24T10:28:54.207Z",
                "FourMonthLater": "2019-08-24T10:28:54.207Z",
                "__v": 0
            }
        ]
    }
]

但是我希望他们看起来像这样

每个单词应该有不同的事件

[
    {
        "_id": "5cc03a8381bafb40b884e0e6",
        "word": "Mouse",
        "translate": "Fare",
        "kind": "İsim",
        "exampleSentence": "asdasasf",
        "__v": 0,
        "events": [
            {
                "_id": "5cc03ac581bafb40b884e0e7",
                "createdAt": "2019-04-24T10:28:54.207Z",
                "TenMinutesLater": "2019-04-24T10:29:04.207Z",
                "OneWeekLater": "2019-05-01T10:28:54.207Z",
                "OneMonthLater": "2019-05-24T10:28:54.207Z",
                "FourMonthLater": "2019-08-24T10:28:54.207Z",
                "__v": 0
            }
        ]
    },
    {
        "_id": "5cc03ae081bafb40b884e0e8",
        "word": "Keyboard",
        "translate": "Klavye",
        "kind": "İsim",
        "exampleSentence": "asdbaksbdasd",
        "__v": 0,
        "events": [
            {
                "_id": "5cc03ae881bafb40b884e0e9",
                "createdAt": "2019-04-24T10:28:54.207Z",
                "TenMinutesLater": "2019-04-24T10:29:04.207Z",
                "OneWeekLater": "2019-05-01T10:28:54.207Z",
                "OneMonthLater": "2019-05-24T10:28:54.207Z",
                "FourMonthLater": "2019-08-24T10:28:54.207Z",
                "__v": 0
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:0)

如果您使用的是猫鼬,它内置了populate方法。请考虑将您的Word模式更改为:

const WordSchema = new Schema({
    word:{
        type:String,
    },
    translate: {
        type:String,
    },
    kind:{
        type:String,
    },
    exampleSentence: {
        type:String,     
    },
    isCorrect: {
        type:Boolean
    },
    event: {
        type:mongoose.Schema.ObjectId,
        ref: 'Test',
        required:true
    }
});

然后执行操作(其中Word是对WordSchema的引用),而不是使用查找:

router.get("/", async (req,res) => {
    const data = await Word.find().populate('Test').exec();
    res.json(data);
}) 

填充将自动为您填充参考。