I am looking for a way to create a MongoDB 3.4+ view using pymongo from the following pipeline:
db.getCollection('parsed_tests').aggregate([{
$lookup: {
from: "raw_tests",
localField: "repository_path",
foreignField: "repository_path",
as: "raw_data"
}
}])
I want to do it in Python to have an initialization script in one piece. Has anyone managed to do this?
As sstyvane says, you'll need to give an explicit database command. Unfortunately, they got the syntax wrong.
This worked for me:
db.command('create', 'parsed_and_raw_tests',
viewOn='parsed_tests',
pipeline=my_pipeline)
Also, if you've got a Mongo instance that upgraded from version 3.2 or earlier, you'll need to set the feature compatibility version to 3.4 before you can create a view.
PyMongo does not provide a Database method to create a view. However you can run the create command to create your view with the command method. If fact createView is just a wrapper around the create command.
db.command({
"create": "parsed_tests_view",
"viewOn": "parsed_tests",
"pipeline": pipeline
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With