排序有两种猫鼬架构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