mongodb learning documentation:
https://docs.mongoing.com/
1. Database operation
1. Use the database if it exists, and create it if it doesn’t exist
use test1;
View all databases
show dbs;
Switch current database
use test1;
View current database
db
View all collections in the current database
show tables;
View all the data in the collection (when querying conditions, if you use a field that is not in the collection, no error will be reported, but no result)
db.students.find({})//花括号可加可不加
Delete database
在当前数据库下运行db.dropDatabase()(有表也可以删除,而hive有表的话就不能删除数据库)
Delete collection (nosql and hive can be deleted directly regardless of whether there is data in the table)
db.users.drop();
Delete all documents in the collection
db.inventory.deleteMany({}) 必须带着花括号
update修改已有的文档可以同时新增字段
insert添加新的文档同时可以新增字段
Two, insert operation
insert({})插入空的时候必须带花括号
format
db.collection.insertMany(
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
1... The collection does not need to be pre-defined, the inserted data is automatically created, and the field creation is not limited. At the same time, you can add new fields at will or not insert the value of a field
db.students.insert({
name:'zb1',
age:1001,
hello:1
}
)
2...Insert action uses insert() function db.collection_name.insert()
3. Insert time, support js, new Date()
db.students.insert({
name:'zb11',
age:10011,
hello:11,
sex:"male",
addtime:new Date()//系统时间(格林威治时间,晚8小时),服务器时间不可修改,前端时间可以修改
}
)
db.students.find()
4. To insert multiple records, pay attention to [] in the array document, even if you insert one, use insertmany() to use []
db.students.insertMany([{
name:'zb11',
age:10011,
hello:13,
sex:"male",
addtime:new Date()//系统时间(格林威治时间),服务器时间不可修改,前端时间可以修改
},
{
name:'zb11',
age:10011,
hello:14,
sex:"male",
addtime:new Date()//系统时间(格林威治时间),服务器时间不可修改,前端时间可以修改
}]
)
5.insert = insertmany, insert only one
Three, update
3.1 modifier: followed by (key:value)
{$set:{
status:'pending'}}
1.$set如果存在这个字段则更新,不存在则创建
2.$currentDate()更新创建时间,不存在则自动创建 修改器:
3.$inc 更新数字字段并加n ,n必须是整数 ,1是加1,-1是减1
3.2 Queryer: The front is the key, and the root condition is vlaue in the back
{age:{$gte:10}}
等于: :/$eq
大于:$gt
大于等于:$gte
小于:$lt
小于等于:$lte
不等于:$ne
format:
db.collection.updateMany(
<filter>,相当于sql的where
<update>,相当于sql的set
{
upsert: <boolean>,//默认false,ture表示没有这个条文档的话自动创建
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // Available starting in MongoDB 4.2.1
}
)
updateMany(),
1. Modifier: set, add a field implementation and assign a value, set, add a field implementation and assign a value,set,Plus Yi Ge word segments of the real current and assigned value , the SET if the existence of this field is updated, it does not exist create
db.students.updateMany({},{$set:{sex:'male'}})
2. Modifier: $currentDate updates the creation time, if it does not exist, it will be created automatically
db.students.updateMany({
status:'stop'},{$currentDate:{createtime:true}})
3.update, updateOne only updates one
db.students.updateOne({age:11},{$set:{age:10,status:'ok'}})
4.updateMany()更新多条 == update students set status=“stop” where status=“ok”
db.students.updateMany({
status:'ok'},{$set:{
status:'stop'}})
5.replaceOne(), replace the previously matched document with the second document
db.students.replaceOne({
status:'ok'},{
status:'stop',name:"xiao qi" ,age:10,num:1})
6. If this document cannot be matched before {upsert: true}, a new document will be created automatically
db.students.replaceOne({
status:'o'},{
status:'stop',name:"xiao qi" ,age:10,num:1},{upsert:true})
7. All the students whose age is greater than or equal to 20 are changed to pending
db.students.updateMany(
{age:{$gte:10}},{$set:{
status:'pending'}}
)
8. Change the age of students older than 10 years to +1 and record the modification time. Use the modifier $inc to update the number field and add n
db.students.updateMany(
{age:11},{$inc:{age:1},$currentDate:{createtime:true}}
)
9. Add a field hobby for Xiao Ming (aihao)
db.students.updateMany(
{
stuname:"小明"
},
{
$set:{aihao:"足球,篮球,乒乓球"}
}
)