MongoDB setup and simple operations (in Windows)

Source: Internet
Author: User
Tags findone install mongodb mongodb setup

I saw MongoDB in fish yesterday, but encountered many problems. So I tried it step by step and summarized the problems and solutions.

First, thanks to fish! Yesterday I forgot to put the link: http://www.cnblogs.com/fish-li/archive/2011/06/26/2090800.html

I haven't updated my blog for nearly a year, but I open my blog and read your blog every day. It's just like a civil servant getting used to drinking tea and reading newspapers every day...

Try to publish two articles each month.ArticleI did a job without a blog in the past year, www.5w.cn. Thank you for your appreciation. The front-end uses mvc2.0, spark engine, Entity Framework, and jquery.

After this article is completed, a simple addition, deletion, modification, and query using C # for MongoDB will be launched this week. The demo will appear in the form of MVC + spark. Why are few people using spark, such a good thing is much better than razor, but there is no smart prompt ~

You can download to MongoDB: http://www.mongodb.org/downloads at this address
My environment 32-bit win7 flagship film, so download to mongodb-win32-i386-1.8.2
At the bottom of the download page, the official drivers for various development languages are available. I am C #, direct shift to https://github.com/samus/mongodb-csharpdownload.
1. Install MongoDB
1, unzip the mongodb-win32-i386-1.8.2 to E: \ myprogram \ mongodb-win32-i386-1.8.2 (you can follow your own path, it is best not to have spaces in the path, or else trouble)
2, new folder C: \ data \ dB, This is the default data folder of MongoDB, you can also create a folder data in the folder E: \ myprogram \ mongodb-win32-i386-1.8.2 as the MongoDB database file storage directory
3, Enter cmd, Run Command E: \ myprogram \ mongodb-win32-i386-1.8.2 \ bin \ mongod.exe-dbpath E: \ myprogram \ mongodb-win32-i386-1.8.2 \ data, OK, mongoDB has been installed successfully and is running. You will see the following:

This is the running state of MongoDB. You can press Ctrl + C to end the running state or directly turn off cmd to end the running.
2. Errors and troubleshooting during MongoDB running
after the installation is completed, all cmd windows are closed and then the following operations are performed:
1. Run E: \ myprogram \ mongodb-win32-i386-1.8.2 \ bin \ Mongo, may report an error: couldn't connect to server 127.0.0.1 shell/cmd.js, the original cause is mongod.exe not started,
2, since not started, then let's start it, run E: \ myprogram \ mongodb-win32-i386-1.8.2 \ bin \ mongod, may report an error: dbpath (/data/DB/) does not exist, terminating, look like this, I think I have to create another DB folder under the data folder. In this case, create a DB folder and run the mongod command. The result is the same. I don't know how to do this, google it
Find the article http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
said "to start Mongo in default mode, where data will be stored in the/data/DB directory (or C: \ data \ dB on Windows), and listening on port 27017 ", oh, the original default document path is in C: \ data \ dB. If you don't want to say anything, create a folder directly based on this, run mongod again. OK. the startup is successful. The command window cannot be closed. Otherwise, MongoDB will exit.

3. Run the Mongo command
keep the running status of the mongod command window, and open a new command window
enter E: \ myprogram \ mongodb-win32-i386-1.8.2 \ bin \ Mongo,
MongoDB shell version: 1.8.2
connecting to: Test
OK. The connection is successful.
1. Show DBS displays the Database List
2. Use northwind to enter the northwind database. Case sensitivity
3. Show collections shows the database set.
4, DB is used to view the current database
5, DB. customer. count () view the total number of records of the customer set
6, DB. customer. findone ({"_ id": "1"}) view customerid = 1 records

Add data
1. Use mytest. This database does not exist. Mongo will create it,
Before using MongoDB, you are not required to create a database in advance and design the data table structure!
In MongoDB, there is no [Table] concept. Instead, it is [set], and there is no [Data Record] concept. Instead, it is [document ], we can understand [document] as an [object], any object, or even a complex nested hierarchy.
Therefore, we do not need to write any moreCodeThe conversion from "data table field" to "attribute, field" of the C # class allows you to directly read and write the entire object.
MongoDB does not support join operations. Therefore, if you have a [join] operation, you must handle it yourself.
2, item = {"key": "1", "text": "wokao", "Number": 3}
3. DB. table1.insert (item), Mongo will create the set Table1 and insert the item to complete the work of adding a new database.
4. DB. table1.find () displays the data in Table1. The MongoDB document uses an object called bson format, which is similar to JSON in JavaScript.
5. In addition, enter Item1 = {"ID": 5, "str": "ASDFASDF"} and insert the database. table1.insert (Item1) and run the find () command to check whether the insert operation is successful ~, Note that the structure and item are different! However, this is not recommended.
Note: [each document has a member named "_ id". I have no definition.
In fact, MongoDB creates such a document member for each document. The specified "key" and "ID" are not the [primary key of the document] for MongoDB ], mongoDB only recognizes "_ id". You can specify this parameter. If this parameter is not specified, MongoDB is automatically added.

Modify data
1, var t = dB. table1.findone ({"ID": 5}), get a record
2, T. Str = "wokao"
3, DB. table1.update ({"ID": 5}, T)

Delete data
DB. table1.remove ({"ID": 5 })

Search for Data
The preceding find and findone commands are available for query.
DB. table1.find ()
The <, >=, <= operators are not found in the query conditions of MongoDB. Instead, they use "$ lt", "$ LTE", "$ gt ", "$ GTE"

Create a table
DB. mytest. table2.save ({})

Delete table
DB. table1.drop () or db. runcommand ({"Drop", "Table1 "})

Delete Database
DB. runcommand ({"dropdatabase": 1}), this command can only delete the current database

Get server status information
DB. runcommand ({"serverstatus": 1 })

For more information, see
Http://www.mongodb.org/display/DOCS/Home

The following is an example of how to go to https://github.com/samus/mongodb-csharpto

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.