The operation CRUD MongoDB MongoDB - 2

We   MongoDB - 1   learned how to install and deploy in a MongoDB

If you do not see my noble words, then it is time to re-open the client and server-side bar

In this chapter we learn about CRUD on the MongoDB

Native ORM a .MongoDB operation, the SQL statement does not exist

Create a database: Here and general relational databases, you have to build its own database space

Um um um ah, I feel your heart exclaimed, Chou Chou you have not seen the world like these

Yes, MongoDB design more freely, not to think you are creating, use LuffyCity_Com does not exist, so you think you MongoDB is to create and use

This concept must remember clearly Oh, MongoDB if you use the object does not exist, then it means you create the object in oh

Use the object does not exist, on behalf of creating objects we use this fallacy create a table (Collection) try

It seems really not a fallacy, really successfully created a Oldboy's Collection

Then the next step is to add a piece of data in the table (Collection), a how to add it?

The two .MongoDB data inserted (insert insertOne insertMany) of the insert little Collection of all operations

insert: insert one or more pieces of data, with the need to allow the insertion of a number of parameters, this method is not recommended for myself the current government has

db.Oldboy.insert({"name":"DragonFire","age":20})

insertOne: insert a piece of data, the official recommendation

We can see two ways to return value distinct right

insertMany: insert multiple data without parameter control, the official recommendation

 

That's why we inserted pieces of data to LuffyCity_Com.Oldboy in:

复制代码
复制代码
[{
  "name":"DragonFire",
  "age":20    
},
{
 "name":"WuSir",
 "age":19
}]
复制代码
复制代码

这里留下一个数据类型的悬念

插入完成就要查询

三.MongoDB  之  查询数据(find findOne) 之 这里没有findMany

这里不是select,如果你的第一反应是select 证明你关系型数据库没白学

find() 无条件查找:将该表(Collection)中所有的数据一次性返回

db.Oldboy.find({name:"WuSir2b"}) 条件查找:name等于WuSir2b的数据,这里会返回多条结果

说到这里,有的同学不禁要问一下:"_id":ObjectId("乱七八糟一道对看着毫无关系的一对字符串") 是什么,我们插入的时候并没有一个字段(Field)并没有_id这个,

对了这就是MongoDB自动给我们添加到系统唯一标识"_id" 是一个ObjectId 类型,我们会在数据类型中第一个说到他(MongoDB 之 数据类型 最无聊! But 最有用! MongoDB - 3)

findOne()无条件查找一条数据,默认当前Collection中的第一条数据

findOne({age:19}) : 条件查找一条age等于19的数据,如有多条数据则返回更靠前的数据

查询数据的时候,发现了有些数据出现错误了,要修改怎么办呢?

 

四.MongoDB 之 修改数据(update updateOne updateMany) 之 跟insert一样,不推荐update的写法

update({"name":"DragonFire"},{$set:{"age":21}}):根据条件修改该条数据的内容

把name等于DragonFire中的age改为21,这里要注意的是({"条件"},{"关键字":{"修改内容"}}),其中如果条件为空,那么将会修改Collection中所有的数据

关于$set关键字的解释就是,本节最后再说,留个悬念

updateOne({"age":19},{$set:{"name":"WSSB"}}):根据条件修改一条数据的内容,如出现多条,只修改最高前的数据

把age等于19的所有数据中第一条数据的name改为WSSB

updateMany({"age":19},{$set:{"name":"pig_qi"}}):根据条件修改所有数据的内容,多条修改

把age等于19的所有数据中的name改为WSSB

上述中有一个$set的悬念,这个悬念呀,可能要留到再往后一些了

但是$set:{"name":"WSSB"}我还是要解释一下: $set 是update时的关键字,表示我要设置name属性的值为"WSSB"

那么我们之前说过MongoDB的灵活性,没有就代表我要创建,所以说如果该条Documents没有name属性,他就会自动创建一个name属性并且赋值为"WSSB"

更改了半天,我觉得,这些数据我都不想要了,该怎么办呢?

 

四.MongoDB  之  删除数据(remove) 之 如果你什么都不写,你讲失去全部的生命力

remove({}):无条件删除数据,这里要注意了,这是删除所有数据,清空Collection

当然了,我现在还不会操作,你看到的截图是我全部都写完之后的截图,如果跟着操作的话,那么你实在是太傻了,哈哈哈哈哈哈哈哈

 

如果你的数据全没了,那么请再练习一次insertMany([{"name":"DragonFire"}])吧,你多练习一次,总比马上忘记要强吧

remove({"name":"DragonFire"}) : 条件删除name等于"DragonFire"的所有Document

那么到这里呢,增删改查就已经完事儿了

之后我们来说一下MongoDB的数据类型,跟你们透漏一下,MongoDB的数据类型,老(te)有(bie)意(wu)思(liao)了

Guess you like

Origin www.cnblogs.com/hela/p/11282889.html