Difference between revisions of "MongoDB"

From Wasya Wiki
Jump to: navigation, search
(create user)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
  
== Install ==
+
= Install =
 
From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
 
From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
 +
 
From: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
 
From: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
 +
 
From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
 
From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
  
This is probably obsolete.
 
  
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
 
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
 
sudo apt-get update -y
 
sudo apt-get install -y mongodb-org
 
 
=== mongo 5 on rhel ===
 
From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
 
  
 
= Develop =
 
= Develop =
Line 43: Line 37:
  
 
== create user ==
 
== create user ==
   user admin;
+
   use admin;
 
<pre>
 
<pre>
 
db.createUser(
 
db.createUser(
Line 53: Line 47:
 
   }
 
   }
 
)
 
)
 +
</pre>
 +
 +
== add role to a user ==
 +
<pre>
 +
 +
db.grantRolesToUser('<username>', [
 +
  { role: "readWrite", db: "<dbname>" }
 +
], { w: "majority" , wtimeout: 4000 })
 +
 
</pre>
 
</pre>
  
Line 62: Line 65:
 
  db.getCollection('system.profile').find({})
 
  db.getCollection('system.profile').find({})
  
=== add member to cluster ===
+
== add member to cluster ==
 
  operaeventrsX:PRIMARY> rs.add("10.138.96.89")
 
  operaeventrsX:PRIMARY> rs.add("10.138.96.89")
 
  operaeventrsX:PRIMARY> rs.conf()
 
  operaeventrsX:PRIMARY> rs.conf()
  
=== profiling ===
+
== profiling ==
 
Profiling. From: https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/
 
Profiling. From: https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/
  

Latest revision as of 02:15, 10 March 2023

Install

From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

From: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/


Develop

rename a field, or paste one field to another collection-wide

Methods are: updateOne, updateMany or update

db.collection.<update method>(
    {},
    [
        {"$set": {"name": { "$concat": ["$firstName", " ", "$lastName"]}}}
    ]
)

Count Documents

db.collection.aggregate(
  [
    { $group: { _id: null, count: { $sum: 1 } } }
  ]
)

list users

use admin;
db.getUsers();

create user

 use admin;
db.createUser(
  {
    user: "myTester",
    pwd:  passwordPrompt(),   // or cleartext password
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)

add role to a user


db.grantRolesToUser('<username>', [
  { role: "readWrite", db: "<dbname>" }
], { w: "majority" , wtimeout: 4000 })

Performance Tuning

db.getProfilingStatus()
db.getProfilingLevel()
db.setProfilingLevel(1, 10)
db.getCollection('system.profile').find({})

add member to cluster

operaeventrsX:PRIMARY> rs.add("10.138.96.89")
operaeventrsX:PRIMARY> rs.conf()

profiling

Profiling. From: https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/

The profiler writes all the data it collects to the system.profile collection, a capped collection in the admin database.

db.getProfilingStatus()
# db.setProfilingLevel(level, options)
db.setProfilingLevel(1, { "slowms": 500 })
db.enableFreeMonitoring()
use creek_development
db.setProfilingLevel(1, { "slowms": 500 })
db.getCollection('system.profile').find({}).sort({"ts": -1}).limit( 5 )
db.lineitems.getIndexes()