0

I want to get multiple data from multiple documents in MongoDB, I have used aggregate but I also want to show multiple records in the result.

My Documents schema

//below schema contains book Data

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}
});

//below schemas contains historical Data

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()}
})

I want to show my result as

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

BookId contains the "_id" of "BookSchema"

In BorrowedData and UserData I just want to show only two records.

Dave1
  • 48
  • 1
  • 10

0 Answers0