Mongodb (i)

Source: Internet
Author: User
Tags install mongodb mongodb connection string mongodb support

One: Open the installation package bin directory

Two: Open cmd, switch to Bin directory

Three: Install MongoDB on the service, input mongod in cmd window--install

It's Mongod--install, it's written wrong.

Four: Set the configuration file, in the bin directory to create a data folder to wait for storage, and then create a new mongodb.conf, open mongodb.conf Enter the following configuration information, exit save

V: in cmd input MongoDB--config mongodb.conf, perform the previous step of the configuration file

It's Mongod--config mongodb.conf is wrong.

Six: in cmd input MONGO server ip:27017, for example, this example is native install MONGO with IIS, so input MONGO 127.0.0.1:27017

Seven: Browser input localhost:27017, the following sentence indicates the success of the Open

Eight: Do not turn off this cmd. At this time another CMD operation database opened, common commands

Use database name: Switch database
Show DBS shows all databases
Show Collections: Show All Collections
DB: View the currently pointing document
Db. Users: Point to the Users collection
Db. Users.find (): View all data
Db. Users.insert ({Write a JSON string}), you can enter ex:db when you write the command. Users.insert ({id:1,name: ' Bishige ', age:12})
This is the advantage of modeless, which column to add can be, and case-sensitive
Db. Users.remove (): Delete all data
_id value is maintained by the system itself, we do not need to tube, you can refer to the document "MongoDB learning P4" to simply explain
Query data: item=db. Users.find ({"UId": 5})
Modify: Db. Users.update ({condition},{new value})
Delete: db. Users.remove ({condition})
Insert a few more data to prepare for the search
Search: DB. Users.find ({"UId": {"$GT": 8}}) indicates greater than 8,great than
READ: DB. Userinfo.find ({"$or": [{"id": {"$gt": 1}},{"Age": {"$lt": +}]})//id>1 or age<20
Description: Do not iterate when making changes, iteration when deleting

Nine:. NET Operation MongoDB

-". NET Read and write
"1" introduces the assembly

"2" custom type, used to represent entities in the collection, such as person
Defines a property, plus a specific [Bsonid] representation, corresponding to the Objectid type property in the collection

public class person    {        [Bsonid] public        ObjectId Id {get; set;}        public string Name {get; set;}    }

In addition to the ID this field is not quite the same, other defined properties are the same as SQL Server. Because MongoDB's primary key is actually "_id": ObjectId ("A lot of numbers"), the custom id attribute must be added [Bsonid] to convert to ObjectId
Because MongoDB is not the concept of a table
In fact, all are JSON key value pairs, not to define the person this model, add the Name,id these two properties, MongoDB inside there is name,id these two fields, not this
Person p = new person () {Id=objectid.generatenewid (), name= "Bishige"}; Initialize model, Id to be assigned by Objectid.generatenewid ()

"3" creates a connection string in the form "mongodb://127.0.0.1:27017"

Set MongoDB connection string in configuration file <appSettings>    <add key= "MONGO" value= "mongodb://127.0.0.1:27017"/></ Appsettings>

  

"4" creates a server object based on the connection string:
To create a Mongourl object URL from a connection string
Creates a Mongoserversettings object based on a URL object settings
Create Mongoserver Object Server based on settings


"5" Gets the database object MongoDatabase:server.GetDatabase ("user") from the server object, if it is not automatically created
"6" Gets the collection object
Gets the name of the class, as the name of the collection: typeof (Person). Name
Gets the collection object from the database object: DB. Getcollection<person> (collection name), which is automatically created if the collection does not exist
"7" Collection Object method: Insert,update,count,findall returns the collection and then traverses, find uses the query parameter, Findonebyid
Querying using Bson: Query
The return type is MONGOCURSOR<T>, and you can use the foreach traversal
such as: QUERY.GT ("id", 1);//indicates all data with a query ID greater than 1
Turn the collection into Iqueryable<t> by using the AsQueryable () method, and then query with LINQ or Lambda
-"The difference between MongoDB and Redis
1. MongoDB is a document-based database
2, MongoDB support complex query

Ten:. NET Operation Mongodbdemo

Using mongodb.bson;using mongodb.bson.serialization.attributes;using mongodb.driver;using MongoDB.Driver.Builders; Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace mongodbdemo{class Program {static void Main (string[] args) {Person Pson = new            Person () {Id = Objectid.generatenewid (), Name = ' bishige ', age = 12};            String Mongoip = system.configuration.configurationmanager.appsettings["MONGO"];            Mongourl url = new Mongourl (MONGOIP); mongoserversettings setting = new Mongoserversettings (); Do not use this new to come out mongoserversettings setting = Mongoservers Ettings.            Fromurl (URL);//similar to factory-created mongoserver server = new Mongoserver (setting); Remember that Mongoserver is the class that operates database Mongodatabase db = server through it.            Getdatabase ("Hm12"); String collectionname = Pson. GetType (). Name + "s";//object. GetType (). name/fullname-"Gets the name of the object, FullName is the full name, including the namespace, here just want to get the class name for the MongoDB table names (collection name) if (db. Collectionexists (CollectionName)) {//How the collection exists, take the collection out Mongocollect Ion<person> personcollection = db.                Getcollection<person> (CollectionName);                Add model data to the collection Personcollection.insert (Pson);                Query data querybuilder<person> query = new querybuilder<person> (); Imongoquery q1 = query. GT (p = = p.age, 10);//Imongoquery q2 = 10 years old = query. EQ (p = = p.name, "Bishige");//equals imongoquery q3 = query. and (Q1, Q2);//querybuilder This object is very important, adding and removing the change is through it to point out mongocursor<person> cursor = personcollection.find (Q3 ),//find () is equivalent to the SELECT statement//mongocursor inheriting IEnumerable, so find () is a collection of foreach (var person in C                Ursor) {Console.WriteLine (person);              }} else {  If the collection does not exist, create a commandresult result = db.            CreateCollection (CollectionName);        }}} public class Person {[Bsonid] public ObjectId Id {get; set;}        public string Name {get; set;}        public int Age {get; set;} Format the output effect, like the OC Rewrite, public override string ToString () {return string.        Format ("Id:{0},name:{1},age:{2}", Id, Name, age); }    }}

  

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.