MonGo---安装及其基本操作

2023-06-08,,

1、安装Mongo数据库:

  在发布本文的时间官方提供的最新版本是:1.6.5 ,如果不做特殊声明,本教程所用的版本将会是这个版本。

  1. 第一步:下载安装包:官方下载地址←单击此处,如果是win系统,注意是64位还是32位版本的,请选择正确的版本。

  2. 第二步:新建目录“D:\MongoDB”,解压下载到的安装包,找到bin目录下面全部.exe文件,拷贝到刚创建的目录下。

  3. 第三步:在“D:\MongoDB”目录下新建“data”文件夹,它将会为数据存放的根文件夹。

 配置Mongo服务端:

  打开CMD窗口,按照如下方式输入命令:
  > d:
  > cd D:\MongoDB
  > mongod --dbpath D:\MongoDB\data

在浏览器输入:http://localhost:27017/,可以看到如下提示:
  You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number

2、基本操作:

创建\使用数据库:

> use mongotest

switched to db mongotest

显示数据表:

> show tables;

插入数据:

> item={"Key":"1","text":"wokao","number":3}

{ "Key" : "1", "text" : "wokao", "number" : 3 }

> db.wang.insert(item)

查询数据:

> db.wang.find();

{ "_id" : ObjectId("52c7812912eae9cca2c31826"), "Key" : "1", "text" : "wokao", "number" : 3 }

> item={"Key":"1","text":"wokao"}

{ "Key" : "1", "text" : "wokao" }

> db.wang.insert(item)

> db.wang.find();

{ "_id" : ObjectId("52c7812912eae9cca2c31826"), "Key" : "1", "text" : "wokao", "number" : 3 }

{ "_id" : ObjectId("52c7814912eae9cca2c31827"), "Key" : "1", "text" : "wokao" }

> show tables;

system.indexes

wang

> db.system.indexes.find();

{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "mongotest.wang", "name" : "_id_" }

数据删除:

> db.wang.remove({ "_id" : ObjectId("52c7812912eae9cca2c31826")})

> db.wang.find();

{ "_id" : ObjectId("52c7814912eae9cca2c31827"), "Key" : "1", "text" : "wokao" }

数据修改:

> db.wang.find();

{ "_id" : ObjectId("52c7814912eae9cca2c31827"), "Key" : "1", "text" : "wokao" }

> var t=db.wang.findone({"Key" : "1"})

Sat Jan 04 11:42:29.671 TypeError: Property 'findone' of object mongotest.wang is not a function

> var t=db.wang.findOne({"Key" : "1"})

> t.text="wo shi ni yeye!!"

wo shi ni yeye!!

> db.wang.update({"Key" : "1"},t)

> db.wang.find();

{ "_id" : ObjectId("52c7814912eae9cca2c31827"), "Key" : "1", "text" : "wo shi ni yeye!!" }

《MonGo---安装及其基本操作.doc》

下载本文的Word格式文档,以方便收藏与打印。