[英]Write a complex Mongo query in Rails ORM
I want to convert mongo query to rails ORM query for the below json.我想将 mongo 查询转换为以下 json 的 rails ORM 查询。
Rails query is: Rails 查询是:
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
]
},
}
}
])
Please look at this as well:也请看看这个:
https://mongoplayground.net/p/fb9IMkC1fCs https://mongoplayground.net/p/fb9IMkC1fCs
Collection is the corresponding class and this is what I've tried so far, 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
Mongoid does not provide a DSL for constructing aggregation pipelines. Mongoid 不提供用于构建聚合管道的 DSL。 The driver provides a basic helper to perform aggregation pipeline queries, but returns raw data as BSON::Document instances.
驱动程序提供了一个基本的帮助程序来执行聚合管道查询,但将原始数据作为 BSON::Document 实例返回。
https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-aggregation/ https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-aggregation/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.