我想将mongo查询转换为以下json的rails ORM查询。
轨道查询为:
db.collection.aggregate([
{
$match: {
"data.toc.ge.ge._id": "5b"
}
},
{
$unwind: "$data.toc.ge"
},
{
$unwind: "$data.toc.ge.ge"
},
{
$group: {
_id: null,
book: {
$push: "$data.toc.ge.ge._value"
}
}
},
{
$project: {
_id: 0,
first: {
$arrayElemAt: [
"$book",
0
]
},
}
}
])
请同时查看:
https://mongoplayground.net/p/fb9IMkC1fCs
Collection是对应的类,这是我到目前为止尝试过的
unwind2= {'$unwind': "$data.toc.ge.ge"}
unwind3= {'$unwind': "$data.toc.ge.ge.ge"}
group= {'$group': {_id: nil, book: {'$push': "$data.toc.ge.ge.ge._display_name"}}}
match= {'$match': {"data.toc.ge.ge.ge._id": "m121099"}}
project= {'$project': {_id: 0, 'mytopic': {'$arrayElemAt': ["$book",0]},}}
answer = collection.aggregate([match,unwind1,unwind2,unwind3,group,project]).to_a
答案 0 :(得分:0)
Mongoid不提供用于构建聚合管道的DSL。该驱动程序提供了执行聚合管道查询的基本帮助程序,但是将原始数据作为BSON :: Document实例返回。
https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-aggregation/