mongodb 数据库 存储 基础

首先在终端打开mongodb。

一。数据库的创建与打开。

    use DATABASE_NAME(数据库的名字);没有这个数据库就是创建,有就是打开。

例:

>use text
switched to db text
> db
text

 这样就创建了 name=text的数据库。

二。数据库的删除。

  db.dropDatabase();删除当前数据库。

例:

>use text
switched to db text
>db.dropDatabase()

 这样就删除了text数据库。

三。数据库插入信息。

  db.COLLECTION_NAME.insert(documnet).

COLLECTION_NAME:插入集合的名字。

document:插入的数据。

>use text
switched to db text
>db.users.insert({'name':'阿大','age':20})

 这样就把{'name':'阿大','age':20} 插入到 users集合了。

四。数据库数据更新。

 db.COLLECTION_NAME.update(<条件>,<数据>,<没有数据就插入>)

例:

>db.users.update({'name':‘阿大},{'age':24})
>db.users.find()
{'name':‘阿大,'age':24}

 五.数据库的查询。

 db.COLLECTION_NAME.find();

六。数据库数据的删除。

 db.COLLECTION_NAME.remove(<删除文档的条件>,<删除文档(true或1 都是值删除一条数据)>,<抛出错误的级别>);

上面的条件都是可选的。

猜你喜欢

转载自975156298.iteye.com/blog/2315895