我正在寻找一种使用pymongo创建mongodb 3.4+视图的方法。 mongo中的命令如下所示:
db.getCollection('parsed_tests').aggregate([{
$lookup: {
from: "raw_tests",
localField: "repository_path",
foreignField: "repository_path",
as: "raw_data"
}
}])
我想在python中做一个初始化脚本。有没有人设法做到这一点?
答案 0 :(得分:2)
正如sstyvane所说,您需要提供明确的数据库命令。不幸的是,他们的语法错误。
这对我有用:
<h2>fancyBox v3.2 - Basics</h2>
<div id="container"> <!-- to wrap both groups -->
<div id="group1"> <!-- to wrap the images and the heading -->
<h4>Group #1; custom options</h4>
<div class="imglist">
<a href="https://source.unsplash.com/ndjyaOp0fOc/1500x1000" data-fancybox="images"><img src="https://source.unsplash.com/ndjyaOp0fOc/240x160"></a>
<a href="https://source.unsplash.com/A-fubu9QJxE/1500x1000" data-fancybox="images"><img src="https://source.unsplash.com/A-fubu9QJxE/240x160"></a>
<a href="https://source.unsplash.com/rkkr6-2I4sg/1500x1000" data-fancybox="images"><img src="https://source.unsplash.com/rkkr6-2I4sg/240x160"></a>
<a href="https://source.unsplash.com/mr_Tg4SI66A/1500x1000" data-fancybox="images"><img src="https://source.unsplash.com/mr_Tg4SI66A/240x160"></a>
<a href="https://source.unsplash.com/YEsedBccUEA/1500x1000" data-fancybox="images"><img src="https://source.unsplash.com/YEsedBccUEA/240x160"></a>
<a href="https://source.unsplash.com/Hw62tzAkXXE/1500x1000" data-fancybox="images"><img src="https://source.unsplash.com/Hw62tzAkXXE/240x160"></a>
</div>
</div>
<div id="group2"> <!-- to wrap the images and the heading -->
<h4>Group #2; default options</h4>
<div class="imglist">
<a href="https://source.unsplash.com/Lzx4J_Pb3sk/1500x1000" data-fancybox="group2"><img src="https://source.unsplash.com/Lzx4J_Pb3sk/240x160"></a>
<a href="https://source.unsplash.com/cZVthlrnlnQ/1500x1000" data-fancybox="group2"><img src="https://source.unsplash.com/cZVthlrnlnQ/240x160"></a>
<a href="https://source.unsplash.com/vddccTqwal8/1500x1000" data-fancybox="group2"><img src="https://source.unsplash.com/vddccTqwal8/240x160"></a>
<a href="https://source.unsplash.com/Sj5efgWguDs/1500x1000" data-fancybox="group2"><img src="https://source.unsplash.com/Sj5efgWguDs/240x160"></a>
<a href="https://source.unsplash.com/Y7y7fe8hrh0/1500x1000" data-fancybox="group2"><img src="https://source.unsplash.com/Y7y7fe8hrh0/240x160"></a>
<a href="https://source.unsplash.com/sYegwYtIqJg/1500x1000" data-fancybox="group2"><img src="https://source.unsplash.com/sYegwYtIqJg/240x160"></a>
</div>
</div>
</div>
此外,如果您有从3.2或更早版本升级的Mongo实例,则可以在创建视图之前need to set the feature compatibility version to 3.4。
答案 1 :(得分:1)
PyMongo不提供Database
方法来创建视图。但是,您可以运行create
命令以command方法创建视图。如果事实createView
只是create
命令的包装器。
db.command({
"create": "parsed_tests_view",
"viewOn": "parsed_tests",
"pipeline": pipeline
})
答案 2 :(得分:1)
正如MongoDB documentation on views所建议的那样,专用方法createView()
只是createCollection()
中存在的pymongo
方法的包装。
在MongoDB中,创建过程表示如下:
db.createCollection(
"<viewName>",
{
"viewOn" : "<source>",
"pipeline" : [<pipeline>],
"collation" : { <collation> }
}
)
在Python中,您可以通过以下代码实现相同的行为:
# Assuming `db` is your database variable
db.create_collection(
'parsed_tests_view',
viewOn='parsed_tests',
pipeline=[{
'$lookup': {
'from': "raw_tests",
'localField': "repository_path",
'foreignField': "repository_path",
'as': "raw_data"
}
}]
)
有关create_collection()
方法in the docs of pymongo
的更多信息。