英文:
How to convert a db.collection query to a JSON one?
问题
我有以下查询,可以在MONGOSH上成功执行:
db.getCollection("user_products").aggregate( [
{
$lookup:
{
from: "users",
localField: "user_id",
foreignField: "_id",
as: "user"
}
},
{
$lookup:
{
from: "products",
localField: "product_id",
foreignField: "_id",
as: "product"
}
},
{
$unwind: "$product"
},
{
$unwind: "$user"
},
{
"$project": {
"_id": 1,
"creation_date": 1,
"support_all_payment": 1,
"payment": 1,
"imported": 1,
"name": "$user.name",
"country_code": "$user.country_code",
"phone_number": "$user.phone_number",
"wallet_credit": "$user.wallet_credit",
"score": "$user.score",
"birthday": "$user.birthday",
"bio": "$user.bio",
"education": "$user.education",
"email": "$user.email",
"job": "$user.job",
"major": "$user.major",
"sex": "$user.sex",
"have_you_attended_courses_on_personal_and_organizational_success": "$user.have_you_attended_courses_on_personal_and_organizational_success",
"what_books_have_you_read_about_growth_and_development_and_success": "$user.what_books_have_you_read_about_growth_and_development_and_success",
"which_of_the_success_professors_are_you_interested_in_and_what_is_the_reason_for_your_interest": "$user.which_of_the_success_professors_are_you_interested_in_and_what_is_the_reason_for_your_interest",
"city": "$user.city",
"state": "$user.state",
"favorites": "$user.favorites",
"fixed_number": "$user.fixed_number",
"show": "$product.show",
"title": "$product.title",
"type": "$product.type",
"little_description": "$product.little_description",
"description": "$product.description",
"price": "$product.price",
"support_price": "$product.support_price",
"discount": "$product.discount",
}
}
] )
由于我需要导出结果,并且我使用MongoDB Compass,所以我需要将上面的查询转换为JSON格式的{}
。
我尝试过一些在线工具,但没有成功,您有什么方法可以将其转换为JSON吗?
英文:
I have the following query which executes well on the MONGOSH:
db.getCollection("user_products").aggregate( [
{
$lookup:
{
from: "users",
localField: "user_id",
foreignField: "_id",
as: "user"
}
},
{
$lookup:
{
from: "products",
localField: "product_id",
foreignField: "_id",
as: "product"
}
},
{
$unwind: "$product"
},
{
$unwind: "$user"
},
{
"$project": {
"_id": 1,
"creation_date": 1,
"support_all_payment": 1,
"payment": 1,
"imported": 1,
"name": "$user.name",
"country_code": "$user.country_code",
"phone_number": "$user.phone_number",
"wallet_credit": "$user.wallet_credit",
"score": "$user.score",
"birthday": "$user.birthday",
"bio": "$user.bio",
"education": "$user.education",
"email": "$user.email",
"job": "$user.job",
"major": "$user.major",
"sex": "$user.sex",
"have_you_attended_courses_on_personal_and_organizational_success": "$user.have_you_attended_courses_on_personal_and_organizational_success",
"what_books_have_you_read_about_growth_and_development_and_success": "$user.what_books_have_you_read_about_growth_and_development_and_success",
"which_of_the_success_professors_are_you_interested_in_and_what_is_the_reason_for_your_interest": "$user.which_of_the_success_professors_are_you_interested_in_and_what_is_the_reason_for_your_interest",
"city": "$user.city",
"state": "$user.state",
"favorites": "$user.favorites",
"fixed_number": "$user.fixed_number",
"show": "$product.show",
"title": "$product.title",
"type": "$product.type",
"little_description": "$product.little_description",
"description": "$product.description",
"price": "$product.price",
"support_price": "$product.support_price",
"discount": "$product.discount",
}
}
] )
Since I need to get an export of the result, and I use MongoDB Compass, then I need to convert query above to JSON formatted {}
.
I tired some online tools with no success, any idea how can I convert it to JSON?
答案1
得分: 1
如果在mongosh
中该查询有效,而且您正在使用mongosh
,那么您不需要使用Compass将输出导出为JSON。假设您的查询保存在名为q1.js
的文件中,类似于这样:
cursor = db.getCollection("user_products").aggregate([ ... ])
然后只需在输出上调用JSON.stringify()
以生成正确的JSON:
cursor.forEach(function(doc) {
print(JSON.stringify(doc));
});
或者,为了保持类型的一致性(以牺牲更复杂的输出结构为代价),可以使用EJSON:
print(EJSON.stringify(doc));
然后从命令行运行查询:
$ mongosh --quiet mongodb://whereever/dbName q1.js > theOutput.json
英文:
If that query works in mongosh
and you're using mongosh
then you don't need Compass to export the output as JSON. Assume your query is in a file named q1.js
, something like this:
cursor = db.getCollection("user_products").aggregate([ ... ]}
then just call JSON.stringify()
on the output to emit proper JSON:
cursor.forEach(function(doc) {
print(JSON.stringify(doc));
});
or, to preserve type fidelity (at the expense of a more complicated output structure), use EJSON:
print(EJSON.stringify(doc));
and run the query from the command line:
$ mongosh --quiet mongodb://whereever/dbName q1.js > theOutput.json
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论