一、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 > db.help() > db.mycollection.help() > db.mycollection.find().help()
Select Database
The following are three basic commands that provide information about the available databases, and collections in a given database.
> show dbs |
//displays all the databases on the server you are connected to |
> use db_name |
//switches to db_name on the same server |
> show collections |
//displays a list of all the collections in the current database |
Querying
mongo uses a JavaScript API to interact with the database. Because mongo is also a complete JavaScript shell, db is 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:
This will display the first 10 objects from the foo collection. Typing it after a find() will display the next 10 subsequent objects.
|
By setting the shellBatchSize you can change this:
> DBQuery.shellBatchSize = #
|
|
If the shell does not accept the collection name (for example if it starts with a number, contains a space etc), use
instead. |
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
> db.foo.drop() |
//drop the entire foo collection |
> db.foo.remove() |
//remove all objects from the collection |
> db.foo.remove( { name : "sara" } ) |
//remove objects from the collection where name is sara |
Indexes
> db.foo.getIndexKeys() |
//get all fields that have indexes on them |
> db.foo.ensureIndex({ _field_ : 1 }) |
//create an index on field if it doesn't exist |
四、MongoDB Shell Reference
Command Line
--help |
//Show command line options |
--nodb |
//Start without a db, you can connect later with new Mongo() or connect() |
--shell |
//After running a .js file from the command line, stay in the shell rather than terminating |
Special Command Helpers
Non-javascript convenience macros:
> help |
//Show help |
> db.help() |
//Show help on db methods |
> db.myColl.help() |
//Show help on collection methods |
> show dbs |
//Print a list of all databases on this server |
> use dbname |
//Set the db variable to represent usage of dbname on the server |
> show collections |
//Print a list of all collections for current database |
> show users |
//Print a list of users for current database |
> show profile |
//Print most recent profiling operations that took >= 1ms |
Basic Shell Javascript Operations
> db |
//The variable that references the current database object / connection. Already defined for you in your instance. |
> db.auth(user,pass) |
//Authenticate with the database (if running in secure mode). |
> coll = db.collection |
//Access a specific collection within the database. |
> cursor = coll.find(); |
//Find all objects in the collection. See queries. |
> 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
> coll.find() |
//Find all. |
> it |
//Continue iterating the last cursor returned from find(). |
> coll.find( criteria ); |
//Find objects matching criteria in the collection. E.g.: coll.find( { name: "Joe" } ); |
> coll.findOne( criteria ); |
//Find and return a single object. Returns null if not found. If you want only one object returned, this is more efficient than just find() as limit(1) is implied. You may use regular expressions if the element type is a string, number, or date: coll.find( { name: /joe/i } ); |
> coll.find( criteria, fields ); |
//Get just specific fields from the object. E.g.: coll.find( {}, {name:true} ); |
> coll.find().sort( {field:1[, field:1] }); |
//Return results in the specified order (field ASC). Use -1 for DESC. |
> coll.find( criteria ).sort( { field : 1 } ) |
//Return the objects matching criteria, sorted by field. |
> coll.find( ... ).limit(n) |
//Limit result to n rows. Highly recommended if you need only a certain number of rows for best performance. |
> coll.find( ... ).skip(n ) |
//Skip n results. |
> coll.count() |
//Returns total number of objects in the collection. |
> coll.find( ... ).count() |
//Returns the total number of objects that match the query. Note that the number ignores limit and skip; for example if 100 records match but the limit is 10, count() will return 100. This will be faster than iterating yourself, but still take time. |
More information: see queries.
Error Checking
> db.getLastError() |
//Returns error from the last operation. |
> db.getPrevError() |
//Returns error from previous operations. |
> db.resetError() |
//Clear error memory. |
Administrative Command Helpers
> db.cloneDatabase(fromhost) |
//Clone the current database from the other host specified. fromhost database must be in noauth mode. |
> db.copyDatabase(fromdb, todb, fromhost) |
//Copy fromhost/fromdb to todb on this server. fromhost must be in noauth mode. |
> db.fromColl.renameCollection(toColl) |
//Rename collection from fromColl to toColl. |
> db.repairDatabase() |
//Repair and compact the current database. This operation can be very slow on large databases. |
> db.addUser(user,pwd) |
//Add user to current database. |
> db.getCollectionNames() |
//get list of all collections. |
> db.dropDatabase() |
//Drops the current database. |
Opening Additional Connections
> db = connect("<host>:<port>/<dbname>") |
//Open a new database connection. One may have multiple connections within a single shell, however, automatic getLastError reporting by the shell is done for the 'db' variable only. |
> conn = new Mongo("hostname") |
//Open a connection to a new server. Use getDB() to select a database thereafter. |
> db = conn.getDB("dbname") |
//Select a specific database for a connection |
Miscellaneous
> Object.bsonsize(db.foo.findOne()) |
//prints the bson size of a db object (mongo version 1.3 and greater) |
> db.foo.findOne().bsonsize() |
//prints the bson size of a db object (mongo versions predating 1.3) |
五、资料链接
分享到:
相关推荐
MongoDB学习总结入门篇.pdf MongoDB是一个基于分布式文件存储的数据库,旨在为WEB应用提供可扩展的高性能数据存储解决方案。下面将对MongoDB的基本概念、特点、使用原理和基本操作进行详细介绍。 1. MongoDB基本...
本篇文章主要介绍了MongoDB的入门学习,包括下载、安装、启动和连接等基础操作。以下是对这些步骤的详细解析: 1. **下载**: 要获取MongoDB,你需要访问官方网站...
本篇文章将深入介绍MongoDB的基础知识,包括它的核心概念、安装与配置、数据模型、查询操作以及一些常用命令。 1. MongoDB的核心概念 - 文档数据库:MongoDB以JSON格式的文档存储数据,每个文档由键值对组成,支持...
- **《Java语言程序设计-基础篇(原书第8版)》** 和 **《Java语言程序设计-进阶篇(原书第8版)》** 分别覆盖了 Java 编程的基础和高级主题,适合不同阶段的学习者。 - **《JAVA并发编程实战》** 专门讲解 Java 并发...
系列课程总结) Axure RP 指南 - v1.1.pdf Docker —— 从入门到实践 - v1.0 Git 教程 - v1.0.pdf (感觉 也挺好可以查看) GitHub 使用手册 - 基础篇 Gradle 实战中文版 - v1.0 Hibernate 教程 - v1.0.pdf IntelliJ ...
#学习内容源于 程序猿提高篇。 PDF的质量都很高,建议打印出来看 环装 或者胶装 AngularJS 中文版 - v1.0 (推荐) Java 程序员眼中的 Linux - v1.0 Learning-Linux (慕课网 linux 系列课程总结) Axure RP 指南 - v1.1...
总结来说,MongoDB 4系列的首篇文章为读者提供了MongoDB的基本概念、主要特性和工具介绍,为后续深入学习MongoDB打下了基础。同时,对于那些想要入门MongoDB的人来说,提供了实用的安装指南和资源推荐。
, 本书是R语言入门后的进阶读物,为用户灵活使用R语言提供思路上的扩展,给出进阶的学习路线。书中内容来自作者在R语言的实际使用过程中的经验总结,其中涉及计算机、互联网、数据库、大数据、统计、金融等领域,...
对于初学者或进阶学习者而言,可以通过这些项目资源快速入门并深入学习相关技术领域。此外,项目作者还提供了交流渠道,鼓励用户之间的互动与合作,共同促进技术进步和个人成长。 综上所述,这份文件不仅介绍了学科...
本篇文章将围绕"pokemon-ig-api"这个项目,详细介绍如何使用JavaScript来创建一个宝可梦主题的社交网络API,并通过MongoDB进行数据存储。 首先,我们需要了解"pokemon-ig-api"的基本概念。这个项目是宝可梦...
【portal图文入门详解】 在IT领域,Portal技术是一种构建企业信息门户的重要手段,它能够集成各种信息资源,提供个性化的访问界面,实现一站式的信息获取和服务。这篇详解将带你走进Portal的世界,了解其基本概念、...
NoSQL数据库学习教程 CAP定律是NoSQL数据库存在的三大基石之一,CAP定律是指在分布式系统中,无法同时满足一致性、可用性和分区容忍性这三个属性。其中,一致性是指所有节点在同一时间看到相同的数据;可用性是指...
【csdn知识整理】 在IT领域,CSDN(China Software ...总结来说,CSDN是一个全方位的学习和交流平台,无论你是编程新手还是经验丰富的开发者,都能在这里找到有价值的信息,不断提升自己的技术水平和行业认知。
首先列举一下本人总结的相关文章,这些覆盖了入门网络爬虫需要的基本概念和技巧:宁哥的小站-网络爬虫 当我们在浏览器中输入一个url后回车,后台会发生什么?比如说你输入http://www.lining0806.com/,你就会看到宁...
《 Meteor 框架入门与应用实践:基于 meteor-boilerplate 模板的解析》 在Web开发领域,Meteor框架以其独特的全栈式设计和实时数据同步能力,为开发者提供了高效构建Web应用的途径。本篇文章将围绕"meteor-...
2. **社区与论坛**:Stack Overflow、GitHub等平台不仅是提问和解答问题的好去处,也经常能看到开发者分享的学习资源和经验总结。 3. **视频分享网站**:B站、YouTube等视频分享网站上有许多免费的教程资源。可以...
2. 数据库:深入学习SQL(结构化查询语言),了解关系型数据库如MySQL、PostgreSQL,以及非关系型数据库如MongoDB、Redis。掌握数据库设计、事务处理、索引优化等技能。 3. 后端框架:选择一种主流的后端框架,如...
提供给需要它的朋友,希望它可以帮助大家更轻松的完成开发的工作. 关于cmlphp的介绍也可以看看我的这篇文章:再来聊聊cmlphp V2.x CmlPHP V2.x 是一个免费的遵循apache协议的全能型php开源框架 CmlPHP V2.x 是...
在本篇报告中,我们将深入探讨 Ruby on Rails 的生态系统,包括以下几个方面: 1. **Ruby 与 Rails:** 介绍 Ruby 语言的基础以及 Rails 框架的核心概念。 2. **企业用户故事:** 分享不同规模的企业如何利用 Rails...