【发布时间】:2017-04-10 03:52:06
【问题描述】:
有两种猫鼬模式
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
}
})
我们如何编写一个 mongo 查询来根据 cmets 的数量查找名著
因此,编写查找查询以根据特定书籍上的 cmets 数量(其中 inEnglish 设置为 true)对书籍进行排序。
JSON 在 mongo 中存储为:
预订 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}})我已经尝试过了,但它不起作用
标签: mongodb mongoose mongodb-query mongoose-schema