-
Notifications
You must be signed in to change notification settings - Fork 1
MongoDB Tips
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.
- 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" })
- 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()
- Drop a database (dropDatabase)
> db.dropDatabase()
- Drop a user (dropUser)
> db.dropUser("tb571")
> db.dropAllUsers()
- Replace an entry in collection (replaceOne)
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 })
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 fieldstudy
's value isBLS
> db.metadata.findOne({ "study": "BLS" }, { "path": 1, "_id": 0 })
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")
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
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