/* 1 */{ "_id" : ObjectId("55e542cd7f8b9aca2b8b4568"), "game_id" : 1103, "tags" : [ { "tag_id" : 10, "name" : "西遊", "rank" : 10, }, { "tag_id" : 123, "name" : "網遊", "rank" : 1, }, ], "time_sort" : 1435306}/* 2 */{ "_id" : ObjectId("55dd8fe67f8b9a25528b64f7"), "game_id" : 6408, "tags" : [ { "tag_id" : 28, "name" : "白領最愛", "rank" : 20, }, { "tag_id" : 123, "name" : "網遊", "rank" : 2, }, ], "time_sort" : 1411574}/* 3 */{ "_id" : ObjectId("55dd91197f8b9a25528b9585"), "game_id" : 18498, "tags" : [ { "tag_id" : 19, "name" : "回合制", "pc_rank" : 120, }, { "tag_id" : 123, "name" : "網遊", "rank" : 3, }, ], "time_sort" : 1437385}
現在只想通過 網遊標籤的rank排序,語句應該怎麼寫
回複內容:
/* 1 */{ "_id" : ObjectId("55e542cd7f8b9aca2b8b4568"), "game_id" : 1103, "tags" : [ { "tag_id" : 10, "name" : "西遊", "rank" : 10, }, { "tag_id" : 123, "name" : "網遊", "rank" : 1, }, ], "time_sort" : 1435306}/* 2 */{ "_id" : ObjectId("55dd8fe67f8b9a25528b64f7"), "game_id" : 6408, "tags" : [ { "tag_id" : 28, "name" : "白領最愛", "rank" : 20, }, { "tag_id" : 123, "name" : "網遊", "rank" : 2, }, ], "time_sort" : 1411574}/* 3 */{ "_id" : ObjectId("55dd91197f8b9a25528b9585"), "game_id" : 18498, "tags" : [ { "tag_id" : 19, "name" : "回合制", "pc_rank" : 120, }, { "tag_id" : 123, "name" : "網遊", "rank" : 3, }, ], "time_sort" : 1437385}
現在只想通過 網遊標籤的rank排序,語句應該怎麼寫
管道查詢然後用unwind打撒tags,再排序
"tags" : [ { "tag_id" : 28, "name" : "白領最愛", "rank" : 20, }, { "tag_id" : 123, "name" : "網遊", "rank" : 2, }, ],
tags數組裡面的兩個元素都有rank欄位,你要根據第一個rank排序還是第二個rank排序?