The first part of the basic chapter Fourth MongoDB query

Source: Internet
Author: User
Tags findone mongodb query mongo shell

Statement: The article is mainly from the "MongoDB actual Combat" a book, the main want to learn MongoDB knowledge through the book, deepen understanding, so write in their own blog, as a record, in the final chapter, there will be a collection of MongoDB database application Java EE Web application.

1. Inquiry record

1.1. General Enquiry

Before we go into the query, let's look at how to return a cursor object from a query that can be queried simply by find (), which returns a collection of arbitrary structures, if the implementation of a particular query is explained later.

Implement the same query above and then output through the while:

> var cursor = db.xuz.find ();
> while (Cursor.hasnext ()) Printjson (Cursor.next ());
{"_id": ObjectId ("54a8a4ff2db3e1b27d0e5203"), "name": "MONGO"}
{"_id": ObjectId ("54a8a5042db3e1b27d0e5204") , "X": 4}
{"_id": ObjectId ("54a8a5a427681683ca2c2466"), "X": 5, "J": 1}
{"_id": ObjectId ("54a8a5a427681683c a2c2467 ")," X ": 5," J ": 2}
{" _id ": ObjectId (" 54a8a5a427681683ca2c2468 ")," X ": 5," J ": 3}
{" _id ": ObjectId ( "54a8a5a427681683ca2c2469"), "X": 5, "J": 4}
{"_id": ObjectId ("54a8a5a427681683ca2c246a"), "X": 5, "J": 5}
{ "_id": ObjectId ("54a8a5a427681683ca2c246b"), "X": 5, "J": 6}
{"_id": ObjectId ("54a8a5a427681683ca2c246c"), "X": 5, "J": 7}
{"_id": ObjectId ("54a8a5a427681683ca2c246d"), "X": 5, "J": 8}
{"_id": ObjectId ("54a8a5a42768168 3ca2c246e ")," X ": 5," J ": 9}
{" _id ": ObjectId (" 54a8a5a427681683ca2c246f ")," X ": 5," J ": ten}

strong>

The example above shows the iterative output of the cursor style, and the. Hasnext () function tells us if there is data, and if so, it can call the next () function.

When we use JavaScript shell, we can use the JS feature, foreach can output the cursor, the following example is to use foreach () to loop the output: foreach () must define a function for each cursor element to invoke.

> Db.xuz.find (). ForEach (Printjson)
{"_id": ObjectId ("54a8a4ff2db3e1b27d0e5203"), "name": "MONGO"}
{"_id": ObjectId (" 54a8a5042db3e1b27d0e5204 ")," X ": 4}
{" _id ": ObjectId (" 54a8a5a427681683ca2c2466 ")," X ": 5," J ": 1}
{" _id ": O Bjectid ("54a8a5a427681683ca2c2467"), "X": 5, "J": 2}
{"_id": ObjectId ("54a8a5a427681683ca2c2468"), "X": 5, "J": 3}
{"_id": ObjectId ("54a8a5a427681683ca2c2469"), "X": 5, "J": 4}
{"_id": ObjectId ("54a8a5a427681683ca2c246a" ), "X": 5, "J": 5}
{"_id": ObjectId ("54a8a5a427681683ca2c246b"), "X": 5, "J": 6}
{"_id": ObjectId ("54a8a5a 427681683ca2c246c ")," X ": 5," J ": 7}
{" _id ": ObjectId (" 54a8a5a427681683ca2c246d ")," X ": 5," J ": 8}
{" _id ": ObjectId ("54a8a5a427681683ca2c246e"), "X": 5, "J": 9}
{"_id": ObjectId ("54a8a5a427681683ca2c246f"), "X": 5, "J" : ten}

In the MongoDB shell, we can also use cursors as arrays:

> var cursor = db.xuz.find ();
> Printjson (cursor[4]);
{"_id": ObjectId ("54a8a5a427681683ca2c2468"), "X": 5, "J": 3}

When using cursors, be aware of memory-intensive issues, especially large cursor objects, which may overflow memory, so you can change the output by iteration, and the following example converts the cursor to the actual array type:

> var arr = db.xuz.find (). ToArray ();
> Arr[4];
{"_id": ObjectId ("54a8a5a427681683ca2c2468"), "X": 5, "J": 3}

Note that these features are only used in the MONGO shell, not all other application drivers are supported, the MongoDB cursor object is not without a snapshot, if there are other users in the collection for the first time or the last call to next (), you may not get the data in the cursor, So make a clear lock on the cursor you want to query.


1.2. Conditional query

Here we already know how to implement a query from the cursor and return the data object, let's see how to query according to the specified conditions.

The following example shows how to execute a SQL-like query and shows how to implement it in MongoDB, which is queried in the MongoDB shell, and of course you can do it with other application drivers or languages:

Sql:select * from Xuz where name= "MONGO";

Mongodb:

> Db.xuz.find ({name: "MONGO"}). ForEach (Printjson);
{"_id": ObjectId ("54a8a4ff2db3e1b27d0e5203"), "name": "MONGO"}

Sql:select * from Xuz where x=4

Mongodb:

> Db.xuz.find ({x:4}). ForEach (Printjson);
{"_id": ObjectId ("54a8a5042db3e1b27d0e5204"), "X": 4}

The query condition is {a:a,b:b,....} Similar to where a==a and b==b and ....

All of the elements are shown above, and of course we can return a specific element, similar to the value of a field in a table, just specify the name of the element in find ({x:4}).

Sql:select J from Xuz where x=4

Mongodb:

> Db.xuz.find ({x:5},{j:true}). ForEach (Printjson);
{"_id": ObjectId ("54a8a5a427681683ca2c2466"), "J": 1}
{"_id": ObjectId ("54a8a5a427681683ca2c2467"), "J": 2}
{"_id": ObjectId ("54a8a5a427681683ca2c2468"), "J": 3}
{"_id": ObjectId ("54a8a5a427681683ca2c2469"), "J": 4}
{"_id": ObjectId ("54a8a5a427681683ca2c246a"), "J": 5}

1.3. FindOne () syntax

For ease of consideration, the MongoDB shell avoids the overhead of cursors, providing a findone () function, which is the same as the Find () function, but returns the first data in the cursor, or returns NULL, or empty data.

As an example, name= "MONGO" can be implemented in many ways, using next () to loop a cursor or as an array to return the first element.

But using the FindOne () method is simpler and more efficient:

> Printjson (Db.xuz.findOne ({name: "MONGO"}))
{"_id": ObjectId ("54a8a4ff2db3e1b27d0e5203"), "name": "MONGO"}


1.4. Limit the number of result sets through limit

If you need to limit the length of the result set, you can call the Limit method

This is a highly recommended way to resolve performance problems by limiting the number of bars to reduce network transmission, such as:

> Db.xuz.find (). Limit (3)
{"_id": ObjectId ("54a8a4ff2db3e1b27d0e5203"), "name": "MONGO"}
{"_id": ObjectId ("54a8a5042db3e1b27d0e5204"), "X": 4}
{"_id": ObjectId ("54a8a5a427681683ca2c2466"), "X": 5, "J": 1}
> Db.xuz.find (). Count ();
63

By the above can be known, the Xuz collection has a total of 63 data, and then set the value of limit after only 3 data query, limit is also a paging query commonly used.


2, change the record

Change the name of the record name to Mongo to Mongo_new.

> db.xuz.update ({name: "MONGO"},{$set: {name: "Mongo_new"}});
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.xuz.find ();
{"_id": ObjectId ("54a8a4ff2db3e1b27d0e5203"), "name": "mongo_new"}


3. Delete records

Remove the record from the collection xuz that the user name is Mongo_new.

> Db.xuz.remove ({name: "Mongo_new"});
Writeresult ({"nremoved": 1})
> Db.xuz.find ();
{"_id": ObjectId ("54a8a5042db3e1b27d0e5204"), "X": 4}
{"_id": ObjectId ("54a8a5a427681683ca2c2466"), "X": 5, "J": 1}
{"_id": ObjectId ("54a8a5a427681683ca2c2467"), "X": 5, "J": 2}
{"_id": ObjectId ("54a8a5a427681683ca2c2468"), "X": 5, "J": 3}
{"_id": ObjectId ("54a8a5a427 681683ca2c2469 ")," X ": 5," J ": 4}
{" _id ": ObjectId (" 54a8a5a427681683ca2c246a ")," X ": 5," J ": 5}
{" _id ": Ob Jectid ("54a8a5a427681683ca2c246b"), "X": 5, "J": 6}
{"_id": ObjectId ("54a8a5a427681683ca2c246c"), "X": 5, "J": 7
{"_id": ObjectId ("54a8a5a427681683ca2c246d"), "X": 5, "J": 8}
{"_id": ObjectId ("54a8a5a427681683ca2c246e") , "X": 5, "J": 9}
{"_id": ObjectId ("54a8a5a427681683ca2c246f"), "X": 5, "J": Ten}

It is verified that there is no record of name Mongo_new.


4. Common Tool Set

MongoDB provides a number of useful tools in the bin directory that provide the convenience of MONGODB in operations management.

  • Bsondump: Dumps files in Bson format into JSON-formatted data.
  • MONGO: The client command line tool, in fact, is also a JS interpreter, support JS syntax
  • Mongod: The database server, each instance starts a process that can fork to run in the background.
  • Mongodump/mongorestore: Data backup and recovery tools.
  • Mongoexport/mongoimport: Data export and Import tool.
  • MONGOFILE:GRIDFS management tool that enables binary file access.
  • MONGOs: Shard Routing, if the sharding feature is used, the application connects MONGOs instead of Mongod.
  • Mongostat: Real-time performance monitoring tool.
  • Mongotop: provides the level of statistical data for each collection.
all of the above tools are described in detail in a later section.

The first part of the basic chapter Fourth MongoDB query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.