Ruby2.3.3 how to get started with MongoDB (MONGO driver version 2.4.3)-Advance preparation & Database connection creation

Source: Internet
Author: User
Tags mongodb driver

Recent knowledge of MongoDB has involved using Ruby to manipulate the MongoDB database, because the reference data used in Ruby MONGO driver version is not 2.3.3, so in terms of operation will be different, so wrote this Ruby2.3.3 operation MongoDB get started, learning method is reference URL https://docs.mongodb.com/ruby-driver/v2.3/quick-start/

Fast learning is mainly divided into the following sections:

    1. Advance preparation

1.1 Local Start MongoDB database, port number 27017

The startup mode in Windows is as follows: first cmd command line mode to the corresponding Mongobin directory, execute Monggob-dbpath path, after successful execution, the result is as follows:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/9D/E3/wKiom1mIH4bQhqIDAAE187FWRC0690.png-wh_500x0-wm_ 3-wmp_4-s_375500904.png "title=" 1.png "alt=" Wkiom1mih4bqhqidaae187fwrc0690.png-wh_50 "/>

After successful execution, continue to execute the MONGO instruction

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/9D/E3/wKiom1mIIEvTkF3jAAA4pjdKrKY834.png-wh_500x0-wm_ 3-wmp_4-s_3441237563.png "title=" 2.png "alt=" Wkiom1miievtkf3jaaa4pjdkrky834.png-wh_50 "/>,

MongoDB Database started successfully

1.2 MongoDB-based driver for Ruby installed

Gem Install Bson

Gem Install MONGO

After the installation is complete, query--local through gem query to see if the installation was successful, and whether the corresponding driver package and dependent package already exist, as shown in:

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/9D/E3/wKioL1mIIZPTCl9nAAA-8Keh6Fk994.png-wh_500x0-wm_ 3-wmp_4-s_2791162856.png "title=" 3.png "alt=" Wkiol1miizptcl9naaa-8keh6fk994.png-wh_50 "/>

1.3 MONGO driver package imported into Ruby script

And added the Require ' MONGO ' statement at the top of the ruby script


2. Create a MongoDB database connection

In the 2.4.3 version of Ruby MongoDB driver, the connection database uses mongo::client, and the operation statements are as follows

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase = ' test ')

or use Client = mongo::client.new (' mongodb://127.0.0.1:27017/test '), where the test is the database name of the connection, the connection statement and the connection success result as shown:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/9D/E4/wKioL1mIJAvgSwTAAACtWmSF16k606.png-wh_500x0-wm_ 3-wmp_4-s_2355972011.png "title=" 4.png "alt=" Wkiol1mijavgswtaaactwmsf16k606.png-wh_50 "/>


3. Go to the corresponding database and get the collection in the Database collection

After you create the above connection, get to the variable client that identifies the connection

Use the Client.database statement to obtain the corresponding database information, that is, the test database

Db=client.database, the execution results are as follows, get to the corresponding database information test:

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/9D/E4/wKiom1mIJPHyYd8sAAAIIQQP2VY202.png-wh_500x0-wm_ 3-wmp_4-s_1213536523.png "title=" 5.png "alt=" Wkiom1mijphyyd8saaaiiqqp2vy202.png-wh_50 "/>

Using Db.collections to get the collection information under the test database, the results are as follows:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/9D/E4/wKioL1mIJWni6HxQAAAep3rFfWU300.png-wh_500x0-wm_ 3-wmp_4-s_1649150774.png "title=" 6.png "alt=" Wkiol1mijwni6hxqaaaep3rffwu300.png-wh_50 "/>

Also use Db.collection_names to get the collection name under the corresponding database

To get the name of a collection under the corresponding test database

Use the following instruction Collection=client[:order], which is used to get the order collection in the test database. At this point, if the collection does not exist under the database, the collection will be created the first time you insert data into the collection.



This article is from the "techfuture" blog, make sure to keep this source http://wanght89.blog.51cto.com/6778304/1954241

Ruby2.3.3 how to get started with MongoDB (MONGO driver version 2.4.3)-Advance preparation & Database connection creation

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.