排序有兩種貓鼬架構MongoDB的聚集和moongoose模式
const bookModel = new Schema({
title : String,
content: String,
author:{
id:{
type: ObjectId,
},
name:{
type: String,
}
}
})
const commentModel = new Schema({
text :String,
bookid :{ { type : ObjectId , required : true } },
inEnglish:{
type: Boolean,
default:false
}
})
我們如何可以寫一個蒙戈查詢找到的意見
數的基礎上著名的書所以,寫找到查詢根據特定書籍上的評論數量(inEnglish設置爲true)對書籍進行排序。
JSON存儲在蒙戈爲:
書JSON-
{
"_id" :ObjectId(58368df330391521247f6aeb),
"title": "Dan Brown",
"content": "COntent of the book"
}
評論JSON-
{
"_id" :ObjectId(24568df330391765434f6fgh),
"text":"Nice Book",
"bookid":"58368df330391521247f6aeb",
inEnglish: true
}
你寫過任何查詢了嗎?我的意思是你有沒有嘗試過某種東西並卡在某個地方 –
'db.comments.aggregate( {$ match:{inEnglish:true}}, {$ group:{_id:{bookId:「$ bookId」},「totalC」:{$ sum:1}}}, {$ sort:{「_id.totalC」:1}})' 我已經試過這個,但它不起作用 – lee