MongoDB - studiofu/brain GitHub Wiki
basic command
> mongo
> db.auth("admin","admin")
> use admin
> show databases
> show collections
> ...
create user admin account for any database
use admin
> db.createUser(
{
user: "useradmin",
pwd: "basic_user_admin",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
need to edit the configuration file to enable the authorization
mongod.conf
security:
authorization: "disabled" => enabled and restart
$ mongo mongodb://<host>:<port>
> db.auth("admin", "adminpassword")
create normal user, need to use "admin" account to create the normal account
> use test
> db.createUser(
{
user: "test",
pwd: "test123",
roles: [ { role: "readWrite", db: "test" } ]
}
)
grant role to users
db.grantRolesToUser('admin',[{ role: "root", db: "admin" }])
db.grantRolesToUser('admin',[{ role: "readWrite", db: "test" }])
create root role
use admin
db.createUser(
{
user: 'admin',
pwd: 'password',
roles: [ { role: 'root', db: 'admin' } ]
}
);
exit;
use admindb.createUser(
{
user: 'admin',
pwd: 'password',
roles: [ { role: 'root', db: 'admin' } ]
}
);
exit;
use test
db.restaurants.insert(
{
"address" : {
"street" : "2 Avenue",
"zipcode" : "10075",
"building" : "1480",
"coord" : [ -73.9557413, 40.7720266 ],
},
"borough" : "Manhattan",
"cuisine" : "Italian",
"grades" : [
{
"date" : ISODate("2014-10-01T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2014-01-16T00:00:00Z"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}
)
db.getCollection("restaurants").find()
db.getCollection("restaurants").find({ "borough": "Manhattan" })
db.restaurants.find( { "address.zipcode": "10075" } )
db.restaurants.find( { "grades.grade": "B" } )
db.restaurants.find( { "grades.score": { $gt: 30 } } )
db.restaurants.find( { "grades.score": { $lt: 10 } } )
db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )
db.restaurants.find(
{ $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] }
)
db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )
show dbs
more testing scripts
var user1 = {
name : "Mark",
age : 18,
bmi : 10
},
count = 1000,
users = [];
for (var i=0;i<count;i++){
users.push(user1);
}
db.user.insert(users,{ordered:false})
var bulk = db.collection.initializeOrderedBulkOp();
bulk.insert( { name: "mark"} );
bulk.insert( { name: "hoho"} );
bulk.execute();
db.user.insert({"name":"mark","age":23});
db.user.insert({"name":"steven","age":23});
db.user.insert({"name":"jj","age":23});
db.user.update({"name":"mark"},{"name":"mark","age":18})
db.user.update({"name":"mark"},{"$set" : { "age" : 18} })
db.user.updateMany({"name":"mark"},{"$set" : { "age" : 18} })
db.home.insert({"id" : 1 ,"like" : 0})
db.home.update({"id" : 1},{"$inc" : {"like" : 1}})
db.home.update({"id" : 1},{"$inc" : {"like" : 1}})
db.home.update({"id" : 1},{"$inc" : {"like" : 1}})
need to install the pymongo through pip first
python -m pip install pymongo
from pymongo import MongoClient
client = MongoClient('mongodb://test:test@localhost:27017/test')
print ("test123")
database = client.test
collection = database.books
rows = collection.find({})
for row in rows:
print(row)
print(row['_id'])
print(row['name'])
print ("test")
collection = database.test
result = collection.delete_many({'name':'test'})
print(result)
collection.insert_one({'name':'test'})
rows = collection.find({})
print("----- test -----")
for row in rows:
print(row)
print("----- update -----")
collection.update_many(
{'name':'test'},
{'$set': {'score': '50'}}
)
rows = collection.find({})
for row in rows:
print(row)
print("---- end test -----")
Mutiple Mongodb connection
http://www.ityouknow.com/springboot/2017/05/08/springboot-mongodb.html
Basic Tutorial
https://ithelp.ithome.com.tw/articles/10185147
Robo 3T