I’m creating views in MongoDB by running a command like this:
mongo mongodb+srv://my-cluster.mongodb.net --username my-user --password my-password view-content.js
where the contents of view-content.js are as follows:
db.getSiblingDB('my-db').createView(
"content-service_contents_view-v1.0",
"content-service_contents",
[ { $project: {
"_id": 1,
"type": 1,
"title": 1,
"description": 1,
"tags": 1,
"isOfficial": 1,
"isPrivate": 1,
"author": 1,
"provider": 1,
"completion": 1,
"createdAt": 1,
"publishedAt": 1,
"linkedUsers": 1,
"linkedAudiences": 1,
"assignedAudiences": 1,
"setDuration": 1,
"cpdCategoryId": 1,
} } ]
)
If the view doesn’t exist then it gets created successfully but I get no output from this command either way, regardless of whether the view already exists or not.
If I run the exact same command in nosqlbooster I get an error along these lines if the view already exists:
{
"message" : "a view 'my-db.content-service_contents_view-v1.0' already exists",
"ok" : 0,
"code" : 48,
"codeName" : "NamespaceExists",
"operationTime" : "Timestamp(1589459143, 1)",
"$clusterTime" : {
"clusterTime" : "Timestamp(1589459143, 1)",
"signature" : {
"hash" : "z2W0lXUrgRtYmzpMWfmMBdRJAxg=",
"keyId" : "6800465257287385089"
}
},
"name" : "MongoError"
}
My problem is that I’m running the mongo shell command as part of a longer Ansible playbook and I want to know whether the view was created or not. For example when doing db.getCollection.updateOne()
I look for an UpsertedId
in the output to see if something changed. I don’t know how to do this when creating views if there is no output at all.