In this post we review the basic steps of start using an existing MongoDB. See this post for installing a MongoDB on a kubernetes cluster.
To add a new document in a a new collection in a new database, we need to perform the following actions (see examples for commands below):
- Use the new db
- Create new user in the new db
- Grant permissions to the new user in the new db
- Switch to the new user
- Add thew new document
Databases Commands
- use db1
Set a database for the next operations
The database is automatically created when we store data in it - show dbs
Show list of databases
Users Commands
- db.runCommand({connectionStatus : 1})
Display current user information - db.createUser({ user: 'alon', pwd: 'alon', roles: [] })
Create a new user - db.grantRolesToUser('alon', [{ role: 'readWrite', db: 'db1' }])
Add permissions to user - db.auth('alon','alon')
Switch to a user in the current database
Documents Commands
- db.collection1.insertOne({name:'John', Age:45})
Insert new document - db.collection1.find()
Get all documents from the collection - db.collection1.find({'Age':45})
Query for specific documents - db.collection1.insertMany([{'Name':"Foo"},{"Name":"Bar"}])
Insert multiple documents
Indexes Command
- db.collection1.createIndex({'name':1})
Create new index
No comments:
Post a Comment