MongoDB学习总结--入门篇
来源:互联网 发布:夏威夷果 知乎 编辑:程序博客网 时间:2024/05/22 06:47
一、MongoDB简介
MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种。它在许多场景下可
用于替代传统的关系型数据库或键/值存储方式。Mongo使用C++开发。
Mongo的官方网站地址是http://www.mongodb.org/,读者可以在此获得更详细的信息。
二、MongoDB安装与配置
第一步:下载安装包-->MongoDb官方下载地址:http://www.mongodb.org/downloads ,如果是win系统,
注意是64位还是32位版本的,请选择正确的版本。
第二步:可以在任意磁盘新建目录,如“F:\MongoDB”,解压下载到的安装包,找到bin目录下面全部.exe文件,拷
贝到刚创建的目录下。
第三步:在“F:\MongoDB”目录下新建“data”文件夹,在其文件夹下新建db文件夹,它将会作为数据存放的根文
件夹,新建“logs ”件夹,在其文件夹新建“mongodb.log”文件存在日志信息。
第四步:配置MongoDb客户端,打开CMD窗口,按照图示输入命令:
如图显示MongoDB数据库服务已经成功启动了,在浏览器输入:http://localhost:27017/,可以看到如下提示:
You are trying to access MongoDB on the native driver port. For http diagnostic access, add
1000 to the port number。
三、MongoDB Shell Operations
shell命令操作语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的。
Help
> help // top level help> db.help() // help on db-specific methods> db.mycollection.help() // help on collection methods> db.mycollection.find().help() // cursor help
Select Database
The following are three basic commands that provide information about the available databases, and collections in a given database.
Querying
mongo uses a JavaScript API to interact with the database. Because mongo is also a complete JavaScript shell, dbis the variable that is the current database connection.
To query a collection, you simply specify the collection name as a property of the db object, and then call the find() method. For example:
> db.foo.find();
This will display the first 10 objects from the foo collection. Typing it after a find() will display the next 10 subsequent objects.
> DBQuery.shellBatchSize = #
> db['foo'].find()
Inserting
In order to insert data into the database, you can simply create a JavaScript object, and call the save()method. For example, to save an object {name: "sara"} in a collection called foo, type:
> db.foo.save({ name : "sara"});
Note that MongoDB will implicitly create any collection that doesn't already exist.
Updating
Let's say you want to change someone's address. You can do this using the following mongo commands:
> person = db.people.findOne( { name : "sara" } );> person.city = "New York";> db.people.save( person );
Deleting
Indexes
Command Line
Special Command Helpers
Non-javascript convenience macros:
Basic Shell Javascript Operations
>coll.remove(objpattern);
//Remove matching objects from the collection.objpattern is an object specifying fields to match. E.g.: coll.remove( { name: "Joe" } );> coll.save(object)//Save an object in the collection, or update if already there.
If your object has a presave method, that method will be called before the object is saved to the db (before both updates and inserts)> coll.insert(object)
//Insert object in collection. No check is made (i.e., no upsert) that the object is not already present in the collection.
> coll.update(...)
//Update an object in a collection. See the Updating documentation; update() has many options.
> coll.ensureIndex( { name : 1 } )//Creates an index on tab.name. Does nothing if index already exists. > coll.drop()//Drops the collection coll> db.getSisterDB(name)//Return a reference to another database using this same connection. This allows for cross database queries. Usage example: db.getSisterDB('production').getCollectionNames()
Queries
More information: see queries.
Error Checking
Administrative Command Helpers
Opening Additional Connections
Miscellaneous
- MongoDB学习总结--入门篇
- php学习笔记9--mongodb的入门知识点总结
- MongoDB 学习开发入门
- mongodb 入门学习
- mongodb 学习----入门 笔记
- MongoDB入门学习
- MongoDB学习笔记(入门)
- MongoDB学习笔记(入门)
- MongoDB学习-入门
- mongoDB学习--入门
- Mongodb的入门学习
- MongoDB学习笔记(入门)
- MongoDB入门学习一
- MongoDB入门学习1
- MongoDB入门学习2
- MongoDB入门学习3
- MongoDB入门学习
- MongoDB学习总结
- mac bash 设置环境变量 错误 打不开 vim 不能使用 pwd 的解决方法
- 设置text-overflow: ellipsis后引起的文本对齐问题
- 3076 翻翻乐
- 深入浅出JMS(一)–JMS基本概念
- Weak vs. Strong Memory Models
- MongoDB学习总结--入门篇
- AIX 环境下取得昨天日期的 Shell
- 小知识点
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
- ARMlinux kernel 启动之start_kernel (二)
- Array Dimensions
- Matlab中image、imagesc和imshow函数用法解析
- 《大数据时代》读书笔记——知道“是什么”就够了,没必要知道“为什么”。我们不必非得知道现象背后的原因,而是要让数据自己“发声”
- jersey2 MappableException 异常出现的情况