设为首页 收藏本站
查看: 1591|回复: 1

[经验分享] Mongodb用户手册-英文版

[复制链接]
YunVN网友  发表于 2018-10-28 10:39:03 |阅读模式

  • Getting the Database
  • Getting A Database Connection
  • Inserting Data into A Collection
  • Accessing Data From a Query
  • Specifying What the Query Returns
  • findOne() - Syntactic Sugar
  • Limiting the Result Set via limit()
  • More Help
  • What Next

Getting the Database
  First, run through the Quickstart guide for your platform to get up and running.

Getting A Database Connection
  Let's now try manipulating the database with the database shell . (We could perform similar operations from any programming language using an appropriate driver.  The shell is convenient for interactive and administrative use.)
  Start the MongoDB JavaScript shell with:
  

  
# 'mongo' is shell binary. exact location might vary depending on
  
# installation method and platform
  
$ bin/mongo
  

  

  By default the shell connects to database "test" on localhost.  You then see:
  

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

  

  "connecting to:" tells you the name of the database the shell is using.  To switch databases, type:
  

  
> use mydb
  
switched to db mydb
  

  

  To see a list of handy commands, type help.


Tip for Developers with Experience in Other Databases  
You  may notice, in the examples below, that we never create a database or  collection.  MongoDB does not require that you do so.  As soon as you  insert something, MongoDB creates the underlying collection and  database.  If you query a collection that does not exist, MongoDB treats  it as an empty collection.

  Switching to a database with the use command won't  immediately create the database - the database is created lazily the  first time data is inserted. This means that if you use a database for the first time it won't show up in the list provided by `show dbs` until data is inserted.

Inserting Data into A Collection
  Let's create a test collection and insert some data into it.  We will create two objects, j and t, and then save them in the collection things.
  In the following examples, '>' indicates commands typed at the shell prompt.
  

  
> j = { name : "mongo" };
  
{"name" : "mongo"}
  
> t = { x : 3 };
  
{ "x" : 3  }
  
> db.things.save(j);
  
> db.things.save(t);
  
> db.things.find();
  
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
  
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
  
>
  

  

  A few things to note :


  • We did not predefine the collection.  The database creates it automatically on the first insert.
  • The documents we store can have any "structure" - in fact in this  example, the documents have no common data elements at all.  In  practice, one usually stores documents of the same structure within  collections.  However, this flexibility means that schema migration and  augmentation are very easy in practice - rarely will you need to write  scripts which perform "alter table" type operations.
  • Upon being inserted into the database, objects are assigned an object>
  • When you run the above example, your ObjectID values will be different.
  Let's add some more records to this collection:
  

  
> for (var i = 1; i  db.things.find();
  
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
  
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
  
has more
  

  

  Note that not all documents were shown - the shell limits the number  to 20 when automatically iterating a cursor.  Since we already had 2  documents in the collection, we only see the first 18 of the  newly-inserted documents.
  If we want to return the next set of results, there's the it shortcut. Continuing from the code above:
  

  
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
  
has more
  
> it
  
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
  

  

  

  Technically, find() returns a cursor object. But in the cases above,  we haven't assigned that cursor to a variable. So, the shell  automatically iterates over the cursor, giving us an initial result set,  and allowing us to continue iterating with the it command.
  But we can also work with the cursor directly; just how that's done is discussed in the next section.

Accessing Data From a Query
  Before we discuss queries in any depth, lets talk about how to work  with the results of a query - a cursor object.  We'll use the simple find() query method, which returns everything in a collection, and talk about how to create specific queries later on.
  In order to see all the elements in the collection when using the mongo shell, we need to explicitly use the cursor returned from the find() operation.
  Lets repeat the same query, but this time use the cursor that find() returns, and iterate over it in a while loop :
  

  
> var cursor = db.things.find();
  
> while (cursor.hasNext()) printjson(cursor.next());
  
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
  
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
  

  The above example shows cursor-style iteration.    The hasNext() function tells if there are any more documents to return, and the next() function returns the next document.  We also used the built-in tojson() method to render the document in a pretty JSON-style format.
  When working in the JavaScript shell, we can also use the functional features of the language, and just call forEach on the cursor.  Repeating the example above, but using forEach() directly on the cursor rather than the while loop:
  

  
> db.things.find().forEach(printjson);
  
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
  
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
  

  

  In the case of a forEach() we must define a function that is called for each document in the cursor.
  In the mongo shell, you can also treat cursors like an array :
  

  
> var cursor = db.things.find();
  
> printjson(cursor[4]);
  
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
  

  

  When using a cursor this way, note that all values up to the highest  accessed (cursor[4] above) are loaded into RAM at the same time.  This  is inappropriate for large result sets, as you will run out of memory.   Cursors should be used as an iterator with any query which returns a  large number of elements.
  In addition to array-style access to a cursor, you may also convert the cursor to a true array:
  

  
> var arr = db.things.find().toArray();
  
> arr[5];
  
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
  

  

  Please note that these array features are specific to mongo - The Interactive Shell, and not offered by all drivers.
  MongoDB cursors are not snapshots - operations performed by you or  other users on the collection being queried between the first and last  call to next() of your cursor may or may not be returned by the cursor.  Use explicit locking to perform a snapshotted query.

Specifying What the Query Returns
  Now that we know how to work with the cursor objects that are  returned from queries, lets now focus on how to tailor queries to return  specific things.
  In general, the way to do this is to create "query documents", which  are documents that indicate the pattern of keys and values that are to  be matched.
  These are easier to demonstrate than explain.  In the following  examples, we'll give example SQL queries, and demonstrate how to  represent the same query using MongoDB via the mongo shell.  This way of specifying queries is fundamental to MongoDB, so you'll find the same general facility in any driver or language.

SELECT * FROM things WHERE name="mongo"  

  
> db.things.find({name:"mongo"}).forEach(printjson);
  
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
  

  


SELECT * FROM things WHERE x=4  

  
> db.things.find({x:4}).forEach(printjson);
  
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
  

  

  The query expression is an document itself.  A query document of the  form { a:A, b:B, ... } means "where a==A and b==B and ...".  More  information on query capabilities may be found in the Queries and Cursors section of the Mongo Developers' Guide.
  MongoDB also lets you return "partial documents" - documents that  have only a subset of the elements of the document stored in the  database.  To do this, you add a second argument to the find() query, supplying a document that lists the elements to be returned.
  To illustrate, lets repeat the last example find({x:4}) with an additional argument that limits the returned document to just the "j" elements:

SELECT j FROM things WHERE x=4  

  
> db.things.find({x:4}, {j:true}).forEach(printjson);
  
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "j" : 1 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "j" : 2 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "j" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "j" : 4 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "j" : 5 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "j" : 6 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "j" : 7 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "j" : 8 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "j" : 9 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "j" : 10 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "j" : 11 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "j" : 12 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "j" : 13 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "j" : 14 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "j" : 15 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "j" : 16 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "j" : 17 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "j" : 18 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "j" : 19 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "j" : 20 }
  

  

  Note that the "_id" field is always returned.

findOne() - Syntactic Sugar
  For convenience, the mongo shell  (and other drivers) lets you avoid the programming overhead of dealing  with the cursor, and just lets you retrieve one document via the findOne() function. findOne() takes all the same parameters of the find() function, but instead of returning a cursor, it will return either the first document returned from the database, or null if no document is found that matches the specified query.
  As an example, lets retrieve the one document with name=='mongo'.  There are many ways to do it, including just calling next() on the cursor (after checking for null, of course), or treating the cursor as an array and accessing the 0th element.
  However, the findOne() method is both convenient and efficient:
  

  
> printjson(db.things.findOne({name:"mongo"}));
  
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
  

  

  This is more efficient because the client requests a single object  from the database, so less work is done by the database and the network.   This is the equivalent of find({name:"mongo"}).limit(1).

Limiting the Result Set via limit()

  You may limit the>  This is highly recommended for performance reasons, as it limits the  work the database does, and limits the amount of data returned over the  network.  For example:
  

  
> db.things.find().limit(3);
  
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
  
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
  
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
  

  


More Help
  In addition to the general "help" command, you can call help on db and db.whatever to see a summary of methods available.
  If you are curious about what a function is doing, you can type it  without the {{()}}s and the shell will print the source, for example:
  

  
> printjson
  
function (x) {
  
print(tojson(x));
  
}
  

  


  mongo is a full JavaScript shell, so any JavaScript  function, syntax, or>
What Next


  • After completing this tutorial the next step to learning MongoDB is to dive into the manual for more details.


  • See also SQL to Mongo Mapping Chart



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-627413-1-1.html 上篇帖子: windows环境运行MongoDB 下篇帖子: mongodb备份问题
累计签到:110 天
连续签到:1 天
发表于 2018-12-21 21:18:45 | 显示全部楼层
好东西,值得顶!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表