当尝试使用 MongooseJs 在 Mongodb 中按嵌套数组进行排序时,我遇到了一个小问题。
a) 一个产品包含任务,每个任务又包含子任务。
b) 任务有一个顺序,每个子任务也有顺序(task.order & task.subtask.order)。
这是产品文档示例:
db.products.find({_id: ObjectId("554a13d4b692088a38f01f3b")})
Run Code Online (Sandbox Code Playgroud)
结果:
{
"_id" : ObjectId("554a13d4b692088a38f01f3b"),
"title" : "product title",
"order" : 3,
"description" : "Description here ",
"status" : "live",
"tasks" : [
{
"title" : "task 1",
"description" : "task 1 desc",
"order" : 10,
"_id" : ObjectId("554a13d4b692088a38f01f3a"),
"status" : "live",
"subTasks" : [
{
"title" : "task 1 sub 1",
"content" : "aaa",
"order" : -2,
"_id" : ObjectId("554a13d4b692088a38f01f5a"),
"status" : "live"
},
{ …Run Code Online (Sandbox Code Playgroud)