Skip to content

MongoDB Tips

Tashrif Billah edited this page Sep 16, 2021 · 17 revisions

For information about the different collections in the dpdata MongoDB database, see MongoDB Structure.

In the following MongoDB shell commands, metadata has been used as a collection name.

Search

  • Find all collections

https://docs.mongodb.com/manual/reference/method/db.getCollectionNames/

db.getCollectionNames()

https://docs.mongodb.com/manual/reference/method/db.collection.find/

  • Find all documents in a collection
> db.metadata.find()
  • Find just one document in a collection
> db.metadata.findOne({ "study": "BLS" })

Deletion

  • Delete all documents in a collection (remove)
> db.metadata.remove({})
  • Delete all documents in a collection matching a query condition (remove)
> db.metadata.remove({ "study": "BLS" })
  • Delete the first document in a collection matching a query condition (deleteOne)
> db.metadata.deleteOne({ "study": "BLS" })
  • Drop a collection (drop)
> db.metadata.drop()
> db.dropDatabase()
> db.dropUser("tb571")
> db.dropAllUsers()

Replacement

In the case where a user has mistakenly generated an ID or given access to a site, it can be revoked as:

> db.subjectids.replaceOne({ "_id" : ObjectId("613a6e0f56c48b671fc82765")}, { "_id" : ObjectId("613a6e0f56c48b671fc82765"), "id" : "CA65122", "site" : "CA", "index" : 5, "used" : false })

Projection

A projection operation will return only the fields you specify, as well as _id unless you specifically exclude it.

  • Return just the path and _id fields from all documents in the collection
> db.metadata.find({}, { "path": 1 })
  • Return just the path field from the first document where the field study's value is BLS
> db.metadata.findOne({ "study": "BLS" }, { "path": 1, "_id": 0 })

Distinct

https://docs.mongodb.com/manual/reference/method/db.collection.distinct/

  • Return the distinct values for a specific field in a collection
> db.metadata.distinct("study")

Export

https://docs.mongodb.com/database-tools/mongoexport/#mongoexport

  • Export a collection as json or csv
mongoexport --uri="mongodb://username:password@localhost:27017/idgen" --collection=subjectids --out ids.json

mongoexport --uri="mongodb://username:password@localhost:27017/idgen" --collection=subjectids --out ids.csv --fields "id,site" --type=csv

Shell

mongo mongodb://username:password@localhost:27017/idgen

For TLS/SSL enabled mongod server e.g. the mongod that is part of DPdash:

mongo --tls --host `hostname` --tlsCAFile ${dpstate}/ssl/ca/cacert.pem --tlsCertificateKeyFile ${dpstate}/ssl/mongo_client.pem
mongo --tls --tlsCAFile ${dpstate}/ssl/ca/cacert.pem --tlsCertificateKeyFile ${dpstate}/ssl/mongo_client.pem mongodb://username:password@rc-predict.partners.org:27017/dpdmongo?authSource=admin

The latter command is useful for accessing a mongod server remotely. A use case is where the Mongo database lives in a different host from the actual data. Make sure to configure firewalld so the remote mongod server is accessible:

firewall-cmd --add-port=27017/tcp --permanent
firewall-cmd --reload