-
Notifications
You must be signed in to change notification settings - Fork 0
mongoDB shell
E. Lynette Rayle edited this page Oct 24, 2023
·
2 revisions
Reference: https://www.mongodb.com/developer/products/mongodb/cheat-sheet/
brew install mongosh
$ mongosh "CONNECTION_STRING"
globaldb [primary] test>
Type exit
to exit mongosh
show dbs
use DATABASE_NAME
db
???
show collections
n/a - record is a json structure that can vary from record to record (I think)
db.createUser({"user": "root", "pwd": passwordPrompt(), "roles": ["root"]})
???
db.dropDatabase()
db.coll.drop()
db.dropUser("root")
Example because there isn't a generic simple command.
// Create collection with a $jsonschema
db.createCollection("contacts", {
validator: {$jsonSchema: {
bsonType: "object",
required: ["phone"],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType: "string",
pattern: "@mongodb\.com$",
description: "must be a string and match the regular expression pattern"
},
status: {
enum: [ "Unknown", "Incomplete" ],
description: "can only be one of the enum values"
}
}
}}
})
db.COLLECTION_NAME.findOne() - returns first
db.COLLECTION_NAME.find() - returns next 20 results
db.COLLECTION_NAME.find({name: "Max", age: 32}) - all that match name=Max && age=32 (implicit logical "AND")
NOTE: COLLECTION_NAME is either a single word (e.g. curations
) or a hyphenated name (e.g. ['definitions-trimmed']
)
db.COLLECTION_NAME.countDocuments()
db.COLLECTION_NAME.estimatedDocumentCount()