mongodb primary combat

2010-12-27  来源:本站原创  分类:Database  人气:103 

Reserved Address: http://www.blogjava.net/envoydada/archive/2010/06/23/324266.html

Database connection

Now we are through the shell to the database about the actual operation. (Note: Any programming language can be a suitable driver for similar operations. just the way shell interaction more convenient.)

Run MongoDB JavaScript shell:

$ bin/mongo

(By default shell will be connected to the machine (localhost) database) you will see:

MongoDB shell version: 0.9.8
url: test
connecting to: test
type "help" for help
>

"Connecting to:" The data show that the shell connection name. To switch the database:

> use mydb

Enter help to see a simple list of commands.

Experience with other databases to developer tips <br /> In the following example, you might notice that we did not create the database and set. MongoDB is not used to do so. Once you insert the content, MongoDB will create a corresponding set and database. If that does not exist query set, Mongo will be treated as the empty set.

Insert data to the centralized

We build a test set, and then insert some data. We will create two objects j and t, and then stored in the things set.

The following example, the '>' indicates a command prompt shell

> j = { name: "mongo"};
{"name" : "mongo"}
> t = { x : 3 };
{ "x" : 3 }
> db.things.save(j);
> db.things.save(t);
> db.things.find();
in cursor for : DBQuery: example.things ->
{"name" : "mongo" , "_id" : "497cf60751712cf7758fbdbb"}
{"x" : 3 , "_id" : "497cf61651712cf7758fbdbc"}
>

Note:

  • We do not pre-defined set. The first time the database will automatically create a set of insertion.
  • We can have any documents that are stored in different structures. In fact, in this case, the document is no common data between the elements. In practice, the document is usually kept in the same structure have been set inside. This flexibility means that are very easy to move or expand. Almost no need to write scripts to perform actions such as "alter table" sort of operation.
  • Once inserted into the database, the object is assigned an ID (If not it) is stored in the _id field
  • When you run the above example, your value will be different ObjectID.

Beyond that there add the following set of records:

> for( var i = 1; i < 10; i++ ) db.things.save( { x:4, j:i } );
> db.things.find();
in cursor for : DBQuery: example.things ->
{"name" : "mongo" , "_id" : "497cf60751712cf7758fbdbb"}
{"x" : 3 , "_id" : "497cf61651712cf7758fbdbc"}
{"x" : 4 , "j" : 1 , "_id" : "497cf87151712cf7758fbdbd"}
{"x" : 4 , "j" : 2 , "_id" : "497cf87151712cf7758fbdbe"}
{"x" : 4 , "j" : 3 , "_id" : "497cf87151712cf7758fbdbf"}
{"x" : 4 , "j" : 4 , "_id" : "497cf87151712cf7758fbdc0"}
{"x" : 4 , "j" : 5 , "_id" : "497cf87151712cf7758fbdc1"}
{"x" : 4 , "j" : 6 , "_id" : "497cf87151712cf7758fbdc2"}
{"x" : 4 , "j" : 7 , "_id" : "497cf87151712cf7758fbdc3"}
{"x" : 4 , "j" : 8 , "_id" : "497cf87151712cf7758fbdc4"}
has more

Note that all documents are not listed - shell will default to display 10. There are two documents which have been inside the set, so there can only be inserted in the first 8 to see the new documents.

Then, if they wish to see results, you can use it. Then down the example above:

{"x" : 4 , "j" : 7 , "_id" : "497cf87151712cf7758fbdc3"}
{"x" : 4 , "j" : 8 , "_id" : "497cf87151712cf7758fbdc4"}
has more
> it
{"x" : 4 , "j" : 9 , "_id" : "497cf87151712cf7758fbdc5"}
{"x" : 4 , "j" : 10 , "_id" : "497cf87151712cf7758fbdc6"}

Usually, find () returns a cursor object, but in the cases above, we are not sure the cursor is movable. So shell automatically move the cursor, and the initial result is returned to us, allowing us to "it" command to move the cursor.

But we still can use the cursor, the next section will discuss how to do so.

Access the data using a query

In-depth discussion of our queries before we take a look at how a cursor object query results. We will use the simple find () query function, which returns a set (table), and then discuss how to create a specific query.

In the use of mongo shell when, in order to see all set (table) in the data, we need to find () operation returns a cursor.

Then repeat the same query, but this time we use the find () returns the cursor, and move the cursor in the while loop:

> var cursor = db.things.find();
> while (cursor.hasNext()) { print(tojson(cursor.next())); }
{"name" : "mongo" , "_id" : "497cf60751712cf7758fbdbb"}
{"x" : 3 , "_id" : "497cf61651712cf7758fbdbc"}
{"x" : 4 , "j" : 1 , "_id" : "497cf87151712cf7758fbdbd"}
{"x" : 4 , "j" : 2 , "_id" : "497cf87151712cf7758fbdbe"}
{"x" : 4 , "j" : 3 , "_id" : "497cf87151712cf7758fbdbf"}
{"x" : 4 , "j" : 4 , "_id" : "497cf87151712cf7758fbdc0"}
{"x" : 4 , "j" : 5 , "_id" : "497cf87151712cf7758fbdc1"}
{"x" : 4 , "j" : 6 , "_id" : "497cf87151712cf7758fbdc2"}
{"x" : 4 , "j" : 7 , "_id" : "497cf87151712cf7758fbdc3"}
{"x" : 4 , "j" : 8 , "_id" : "497cf87151712cf7758fbdc4"}
{"x" : 4 , "j" : 9 , "_id" : "497cf87151712cf7758fbdc5"}
>

The above example shows the use of a cursor, hasNext () function returns the current document object if there are back, and next () function returns a document object. We also used the built tojson () function to the document object into a JSON data format.

The shell is Javascript, so we can also use the features of the language: the cursor can call forEach. Also take the example above, directly on the cursor loop with a forEach () replaced by:

> db.things.find().forEach( function(x) { print(tojson(x));});
{"name" : "mongo" , "_id" : "497cf60751712cf7758fbdbb"}
{"x" : 3 , "_id" : "497cf61651712cf7758fbdbc"}
{"x" : 4 , "j" : 1 , "_id" : "497cf87151712cf7758fbdbd"}
{"x" : 4 , "j" : 2 , "_id" : "497cf87151712cf7758fbdbe"}
{"x" : 4 , "j" : 3 , "_id" : "497cf87151712cf7758fbdbf"}
{"x" : 4 , "j" : 4 , "_id" : "497cf87151712cf7758fbdc0"}
{"x" : 4 , "j" : 5 , "_id" : "497cf87151712cf7758fbdc1"}
{"x" : 4 , "j" : 6 , "_id" : "497cf87151712cf7758fbdc2"}
{"x" : 4 , "j" : 7 , "_id" : "497cf87151712cf7758fbdc3"}
{"x" : 4 , "j" : 8 , "_id" : "497cf87151712cf7758fbdc4"}
{"x" : 4 , "j" : 9 , "_id" : "497cf87151712cf7758fbdc5"}
>

In the {{forEach ()}} we must define the cursor in a document the operation of each function.

In mongo shell , you can also put the cursor as an array:

> var cursor = db.things.find();
> print (tojson(cursor[4]));
{"x" : 4 , "j" : 3 , "_id" : "497cf87151712cf7758fbdbf"}

When such use of the cursor, note that the highest accessed data (in the example above cursor [4]) all of the following data are also loaded into memory. This result is very inappropriate for large, will result in memory not enough. Large number of results returned, the cursor should be used as an iterator.

In addition to array-style access to a cursor, you may also convert the cursor to a true array:
In addition to an array of styles to a cursor, the cursor can be to simply convert the real array:

> var arr = db.things.find().toArray();
> arr[5];
{"x" : 4 , "j" : 4 , "_id" : "497cf87151712cf7758fbdc0"}
  Please note that these array features are [] unique, not all drivers support  .
MongoDB  The cursor does not do snapshots. If you or someone in your query  ,  Precisely, from the first and last call   {{next}}  Between the data were modified  ,  Then the modification may be returned or may not return  .  If want to get a snapshot of the query, then use the mutex  .
h3.   Customized search results
 Now we know how to use the cursor returned by the query object, the following query to see how to customize the results by modifying  .
  Usually, you can create a key match  "query documents"  Way to achieve this  .
  These used to demonstrate than explain. In the following example  ,  We will give examples of SQL queries  ,  And while taking advantage of MongDB the   [mongo shell|mongo - The Interactive Shell]   The query, the query in this way for  MongoDB  Is necessary, and you'll find in any convenient language environment  .
{code:title=SELECT * FROM things WHERE name="mongo"}> db.things.find({name:"mongo"}).forEach(function(x) { print(tojson(x));});
{"name" : "mongo" , "_id" : "497cf60751712cf7758fbdbb"}
>

SELECT * FROM things WHERE x = 4

> db.things.find({x:4}).forEach(function(x) { print(tojson(x));});
{"x" : 4 , "j" : 1 , "_id" : "497cf87151712cf7758fbdbd"}
{"x" : 4 , "j" : 2 , "_id" : "497cf87151712cf7758fbdbe"}
{"x" : 4 , "j" : 3 , "_id" : "497cf87151712cf7758fbdbf"}
{"x" : 4 , "j" : 4 , "_id" : "497cf87151712cf7758fbdc0"}
{"x" : 4 , "j" : 5 , "_id" : "497cf87151712cf7758fbdc1"}
{"x" : 4 , "j" : 6 , "_id" : "497cf87151712cf7758fbdc2"}
{"x" : 4 , "j" : 7 , "_id" : "497cf87151712cf7758fbdc3"}
{"x" : 4 , "j" : 8 , "_id" : "497cf87151712cf7758fbdc4"}
{"x" : 4 , "j" : 9 , "_id" : "497cf87151712cf7758fbdc5"}
>

Query expression itself is a document object, if it is a similar to {a: A, b: B, ...} of the document query object, it means that "where a = A and b = B and ...", for more information on query usage, in the Mongo Developer's Guide to query and cursor section.

MongoDB also allows you to return a "part of the document object", which is stored in the database returns a subset of document. Simply by using the find () function of the second parameter can do this.
For example, in the last find ({x: 4}) in the case, add a function to be able to return only the data column j:

SELECT j FROM things WHERE x = 4

> db.things.find({x:4}, {j:true}).forEach(function(x) { print(tojson(x));});
{"j" : 1 , "_id" : "497cf87151712cf7758fbdbd"}
{"j" : 2 , "_id" : "497cf87151712cf7758fbdbe"}
{"j" : 3 , "_id" : "497cf87151712cf7758fbdbf"}
{"j" : 4 , "_id" : "497cf87151712cf7758fbdc0"}
{"j" : 5 , "_id" : "497cf87151712cf7758fbdc1"}
{"j" : 6 , "_id" : "497cf87151712cf7758fbdc2"}
{"j" : 7 , "_id" : "497cf87151712cf7758fbdc3"}
{"j" : 8 , "_id" : "497cf87151712cf7758fbdc4"}
{"j" : 9 , "_id" : "497cf87151712cf7758fbdc5"}
>

Note that the "_id" field is always returned.

Note that the "_id" column is always returned to.

findOne () -

For convenience, mongo shell (and other drivers) lets you avoid the programming dealing with the cursor, you only need to findOne () function will be able to get a document. findOne () and find () using the same parameters, but it does not return the cursor, but the first returned from the database file, or does not match the entry returns null.

For example, a variety of ways we can retrieve a name for the 'mongo' the document, including the cursor to call next () function (of course, to verify whether the null after completion), or the cursor as an array and then access the array [0] subscript elements.

Regardless of the method, findOne () function or a convenient and efficient:

> var mongo = db.things.findOne({name:"mongo"});
> print(tojson(mongo));
{"name" : "mongo" , "_id" : "497cf60751712cf7758fbdbb"}
>

If only return an object from the database, this method is more convenient, and in the database and network transmission has less work to do. This approach is equivalent to find ({name: "mongo"}). Limit (1).

Using the limit ()

Through the limit () method to specify the maximum number of results returned, so you can control the size of the query results.

Is recommended to use this approach, can improve performance, because it reduces the workload of the database, but also reduces the network traffic. For example:

> db.things.find().limit(3);
in cursor for : DBQuery: example.things ->
{"name" : "mongo" , "_id" : "497cf60751712cf7758fbdbb"}
{"x" : 3 , "_id" : "497cf61651712cf7758fbdbc"}
{"x" : 4 , "j" : 1 , "_id" : "497cf87151712cf7758fbdbd"}
>
相关文章
  • mongodb primary combat 2010-12-27

    Reserved Address: http://www.blogjava.net/envoydada/archive/2010/06/23/324266.html Database connection Now we are through the shell to the database about the actual operation. (Note: Any programming language can be a suitable driver for similar opera

  • MongoDB Primary(编译MongoDB,C++连接MongoDB测试, GLIB INSTALL) 2012-03-26

    C++ Language Center 点击打开链接 C++ driver download 点击打开链接 Scons安装步骤: cd build/scons python setup.py install 编译驱动之前需要安装pcre 和 scons [[email protected]:~/mongo-cxx-driver-v1.8]#scons 经过一段时间的组建,生成libmongoclient.so: [[email protected]:~/mongo-cxx-driver-v1.8]#ls authTest clientTest

  • MongoDB Primary (简要介绍 和 Linux安装MongoDB 并 简单使用) 2012-03-26

    MongoDB介绍: 非关系型数据库,主要为了解决: a) 对数据库高并发的需求 b) 对海量数据的高效率存储和访问的需求 c) 对数据库的高可扩展性和高可用性的需求 缺点: a) 数据库事务一致性需求 b)数据库的写实时性和读实时性需求 c)对复杂的Sql查询,特别是多表关联查询 MongoDB与关系型数据库存储区别: 1.下载MongoDB http://www.mongodb.org/downloads 在我发这篇blog时我用的是1.8.2,用的是Linux 64-bit 2.运行Ser

  • CentOS 6.5系统中使用yum安装MongoDB 2.6 教程 2013-12-25

    这篇文章主要介绍了CentOS 6.5系统中使用yum安装MongoDB 2.6 教程,本文共分5个步骤完成MongoDB的安装,需要的朋友可以参考下 下面我们在CentOS 6.5 x64系统上安装最新的MongoDB 2.6.0版. MongoDB v2.4.x版的软件仓库有两个包: 1)mongo-10gen-server 此包里面有最新版的mongod和mongos守护程序以及相关的配置和初始化脚本. 2)mongo-10gen 此包里面有最新版的所有MongoDB工具.这些工具方便你管

  • MongoDB PHP API and Usage 2010-04-06

    The article said how to install php - Mongo expansion, now say a few words MongoDB PHP API and Usage Look at a simple example: <?php $m = new Mongo(); // Here the default connection native port 27017 , Of course you can also connect to remote host as

  • How to Maintain MongoDB the _id java auto-growth fields 2010-04-26

    _id field is MongoDB a particular field, it is of type BSON ObjectId. It is the first attribute of each record, and the attribute value must be unique. BSON ObjectId 12 bytes long, contains 4 bytes of timestamp, 3 bytes of the machine id, 2 bytes 3 b

  • How to maintain MongoDB java in the _id field of automatic growth 2010-04-26

    _id field is MongoDB a particular field, it is of type BSON ObjectId. It is the first attribute of each record, and the attribute value must be unique. BSON ObjectId 12 bytes long, contains 4 bytes of timestamp, 3 bytes of the machine id, 2 bytes 3 b

  • MongoDB Replica Set configuration and deployment problems 2010-08-03

    This article describes the MongoDB Replica Set service node deployment and configuration process, providing deployment scripts and test code. Release notes on the use of specific problems encountered. 1.Replica Set Introduction Replica Set is mongoDB

  • MongoDB study notes of 5 2010-08-16

    Inserting Document-Orientation In the description, MongoDB document-oriented means that the database in primary storage unit is the Collection. Some common data formats such as: JSON, XML, a simple key / value pairs. The documents stored in MongoDB a

  • Visual NoSQL China Road: From MySQL to MongoDB 2010-10-22

    Text / Pan Van Cause Vision China website (www.chinavisual.com) is the largest creative people, professional website. By 2009, with many companies, our products are built CMS and communities in the PHP + Nginx + MySQL on; MySQL uses the Master + Mast

  • [4] MongoDB redundancy Cluster (Replication) 2010-11-07

    Redundancy MongoDB Cluster (Replication) Advantages: 1: Mongo is a JSON document-oriented database, is designed as a real object database, rather than a pure key / value store. 2: MongoDB suitable to describe the characteristics of an individual enti

  • [7] MongoDB patch management (Sharding Administration) 2010-11-08

    [7] MongoDB patch management (Sharding Administration) Here are several very useful command, you can get a slice of some cluster configuration information. Slice on how to configure please see: http://datalife.javaeye.com/blog/805201 A: If it is dete

  • English Translation of wiki MongoDB 2010-11-10

    Original: http://en.wikipedia.org/wiki/MongoDB Official Website: http://wiki.mongodb.org/display/DOCS/Home Note: Translation is only my personal understanding, and must complete and accurate. MongoDB (from "humongous") is an open source, scalabl

  • Wordnik the MongoDB experience 2010-11-15

    Wordnik is an online dictionary and encyclopedia services, in about a year ago, they gradually began to migrate from MySQL to the document database MongoDB, which is famous NoSQL products. Recent Wordnik technical team through official blog to share

  • Zheng explains Linux / MongoDB performance bottlenecks Summary 2010-11-15

    NoSQL 1. High concurrency radis, tokyo, memorycach 10 million / sec Data into the memory is full. 2. Massive Data MongoDB, a first memory, a background thread to write the hard disk. Distributed to several machines in the memory, then the largest har

  • Survey Summary MongoDB 2010-12-23

    Compared with the relational database, MongoDB advantages: ① weak consistency (final agreement), to better ensure the user's access speed: For example, in traditional relational database, a type of operation will be locked COUNT data set can be guara

  • MongoDB System Introduction 2010-12-27

    ================================ [Reserved Address: http://www.osseye.com/?p=536 ] ================================ 1. Knowledge and understanding of MongoDB MongoDB is a set-oriented, model free document database. Set-oriented, meaning that data is

  • Combat DDD (Domain-Driven Design Domain Driven Design) 2011-01-14

    In 2004 the famous modeling expert Eric Evans published his famous books, the most influential: Domain-Driven Design-Tackling Complexity in the Heart of Software (Chinese translation: domain driven design in March 2006 Tsinghua University Press trans

  • Will be imported into a mysql table in the ruby ​​script mongodb 2011-05-21

    Features: The mysql table data into the specified designated mongodb table, the import process to ensure that data is not lost, if the data are updated guidance will be re. Requirements: Source table must have two fields: id: primary key mmm_ts: Last

  • Brief Introduction of management operations MongoDB 2011-08-01

    Property system has been used unwittingly MongoDB more than a year, I remember a year ago, it was touted when NOSQL, MongoDB is as NOSQL in the crowd fired was very hot, it should be on this unit at the time very hot with, so the choice of turning ba