从mongodb中的多个文档中获取数据

时间:2018-04-14 02:18:04

标签: mongodb mongoose find

我想从MongoDB中的多个文档中获取多个数据,我使用了聚合,但我也希望在结果中显示多个记录。

我的文档架构

//架构下面包含书籍数据

var BookSchema = new Schema({
    BookName: { type: String,required: true,index:{unique:true}},
    userAssignedId: {type: Schema.Types.ObjectId},
    BookAuthorId: {type: Schema.Types.ObjectId,required: true},
    BookVersionId :{type: Schema.Types.ObjectId,required: true}
});

//下面的模式包含历史数据

var BorrowedSchema = new Schema({
    BookId :{type: Schema.Types.ObjectId,required: true},
    BorrowedTimeStamp: { type: Date,default:Date.now()},
    BookISBN1: { type: Number,required: true},
    BookISBN2: { type: Number,required: true},
    BookISBN3: { type: Number,required: true}
});

var UserSchema = new Schema({
    UserName :{type: String,required: true},
    BookId: { type: Schema.Types.ObjectId }
    BorrowedTimeStamp: { type: Date,default:Date.now()}
})

我想将结果显示为

Data:{
    BookData: {
        BookId : ''
        userAssignedId: '',
        BookAuthorId: '',
        BookVersionId :'',
        BorrowedData :[{
             BookId : '',
             BorrowedTimeStamp: '',
             BookISBN1: '',
             BookISBN2: '',
             BookISBN3: ''
        },{
             BookId : '',
             BorrowedTimeStamp: '',
             BookISBN1: '',
             BookISBN2: '',
             BookISBN3: ''
        }],
        UserData :[{
                UserName :"",
                BookId: ""
            },{
                UserName :"",
                BookId: ""
            }]
    }
}

BookId包含“BookSchema”的“_id”

在BorrowedData和UserData中我只想显示两条记录。

0 个答案:

没有答案