A talk about MongoDB (i.)

Source: Internet
Author: User
Tags lua mongodb client mongodb driver mongo shell

A talk about MongoDB (i.)


When you start to learn MongoDB, look for some Chinese blogs. Later found that a lot of knowledge can not be searched, the last is to see the official website and the authoritative guide, or the two reliable, free time to browse a bit more. This article summarizes my experience in learning MongoDB.

Mongo Shell Operation MongoDB

After installing the MongoDB client, use Shelll to manipulate MongoDB. I am using the client in Ubuntu, directly with Apt-get to install, relatively simple.

Method 1 Interactive (mysql-like)

Connection format MONGO host:port/dbname–u username–p password

[Email protected]:~/data/test4$ MONGO Localhost:9019/admin–u linger–p 123123

Show DBS;

Show tables;


Method 2 Scripting

Mongo–nodb into MONGO interpreter

> Master=new Mongo ("localhost:9019")

Connection to localhost:9019

> Mdb=master.getdb ("admin")

Admin

> Mdb.auth ("Linger", "123123")

1

> Mdb.getcollectionnames ()

["admin", "system.indexes", "System.users", "Test"]

The following connection methods are also possible

Mdb =connect ("Localhost:9019/admin", "linger", "123123")

Method 3 File-scripted

MONGO < Test.js

Test.js File Contents

Master=new Mongo ("localhost:9019")

Mdb=master.getdb ("admin")

Mdb.auth ("Linger", "123123")

Mdb.getcollectionnames ()

This uses the method of input redirection.

The official website also introduces a method of executing JS,

MONGO Localhost:27017/test Myjsfile.js

But I feel this is not flexible enough, because the host IP and port of the connection and dbname written in the command.

Interactive approach, more like the operation of MySQL.

Scripted approach, more like a programming language.

Interactive and scripted control of some commands


http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/#mongo-shell-new-connections

Simply talk about additions and deletions and change reading

MongoDB processes data in JSON format,

such as inserting data

Db.testTable.insert ({User: "Linger", "age", "24"})

Querying data

Db.testData.find ({User: "Linger"})

For more information on additions and deletions, please refer to the data

Adding and removing changes to read tutorials

http://docs.mongodb.org/manual/applications/crud/

Modify the API Reference manual

http://docs.mongodb.org/manual/reference/method/

Use of MongoDB in application development

The way the shell operates MongoDB, I think is mainly to manage MongoDB, this way can not do application development.

To do application development, we need a programming language and then invoke the MongoDB-related interface via the MongoDB driver.

The description of the driver on the official website is like this

An application communicates with MongoDB by wayof a client library, called a driver, which handles all Interac tion with the database in alanguage appropriate to the application.

http://docs.mongodb.org/manual/applications/drivers/

Simply put, the app interacts with MongoDB through the driver.

The drivers are provided by the authorities and are developed by third parties themselves.

The following picture is the driver for each language


Accidentally discovered on the internet cloud wind brother unexpectedly developed a set of Lua drive. At first he wanted to do a layer of encapsulation in C drive, call Lua, and then implement a copy from the protocol layer. I simply browsed through the code, the Protocol layer is written in C, directly call the Windows socket and server communication, encapsulating the most basic API, and then LUA calls the C package into a variety of functions.

The following passage is from his blog

I once wanted to do encapsulation on the basis of C Driver. Do the process found that C driver code quality is not high, and feature support is not complete, and finally I consider myself from the protocol layer began to re-make a copy.

MONGO's documentation is not complete, I feel the protocol definition is not very rigorous. So in the implementation process encountered a lot of trouble. The original thought that a day can be finished, the result has been done for three days. It's finally possible to release a preliminary version today.

MongoDB's Lua Driver

Http://blog.codingnow.com/2013/06/mongodb_lua_driver.html

Http://blog.codingnow.com/2013/06/lua_mongo.html

Https://github.com/cloudwu/lua-mongo

MongoDB can be used as input and output to Hadoop

MongoDB Connector for Hadoop

The MongoDB Connector for Hadoop are aplugin for Hadoop

that provides the ability to use MongoDB Asan input source and/or an output destination.

http://docs.mongodb.org/ecosystem/tools/hadoop/

About master-Slave separation

MongoDB can be the master from the cluster, called primary or master, from called secondary or slave.

It is important to note that secondary is not read-write by default.

At that time I connected a secondary, wanted to check but made a mistake.

Wed Dec 18:50:04.670 Error: {"$err": "Not Master and Slaveok=false", "Code": 13435} at src/mongo/shell/query.js:128

There are two ways to implement a query from a machine:

The first method: Db.getmongo (). Setslaveok ();

The second method: Rs.slaveok ();

The secondary node in the replica set is not readable by default.

In applications where there is less write-read, the replica sets is used to achieve read-write separation.

By specifying Slaveok at connection time or in the main library,

By secondary to share the pressure of reading,

Primary only assume write operations.

http://wengzhijuan12.blog.163.com/blog/static/3622414520137104257376/

Let MongoDB's secondary support read operations



Replication is the process of synchronizingdata across multiple servers. http://docs.mongodb.org/manual/core/replication-introduction/

Replication used to synchronize each Server data that can be used to make decisions from separation.

About sharding

Sharding is a method for storing data acrossmultiple machines. MongoDB uses sharding to support deployments with very largedata sets and high throughput operations.

http://docs.mongodb.org/manual/core/sharding-introduction/

Simply put, sharding is the function of storing data in multiple machines and scaling up.




About user Permissions

Administrator as the newly added DB,

Administrator identity is also directly unable to login,

Have to log in to the other DB first,

Use the command again to switch to the add DB just now.

If you want to log in directly, add additional users and authorize.

So, I think the user and password are for a specific db, not for MongoDB.

Add a user linger to the database Recsys and add read and write permissions.

Db.adduser ({User: "Linger", pwd: "09388296", roles:["ReadWrite"]})


Reference here

http://docs.mongodb.org/manual/reference/method/db.addUser/#db. AddUser


Additional references:

MongoDB Scripting

http://docs.mongodb.org/manual/administration/scripting/


This article link: http://blog.csdn.net/lingerlanlan/article/details/42150611

This article linger



A talk about MongoDB (i.)

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.