首先从网上下载MongoDB。地址http://www.mongodb.org/,找到适合自己的下载
这是我下载的。
在E盘新建个文件夹,将刚才下载的zip解压,将其中bin目录下的文件全部拷贝至刚才新建的文件夹。
然后在其中再建立个data文件夹。
然后通过cmd去启动你的MongoDB
看我红线框出来的即可。上面打错了 - -
然后访问localhost:27017看到如下所示,就表示你的MongoDB已经启动完毕
增加:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;
using System.Data.SqlClient;
using MongoDB.Bson;
using MongoDB.Driver;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//连接信息
string conn = "mongodb://localhost";
string database = "demoBase";
string collection = "demoCollection";
MongoServer mongodb = MongoServer.Create(conn);//连接数据库
MongoDatabase mongoDataBase = mongodb.GetDatabase(database);//选择数据库名
MongoCollection mongoCollection = mongoDataBase.GetCollection(collection);//选择集合,相当于表
mongodb.Connect();
//普通插入
var o = new { Uid = 123, Name = "xixiNormal", PassWord = "111111" };
mongoCollection.Insert(o);
//对象插入
Person p = new Person { Uid = 124, Name = "xixiObject", PassWord = "222222" };
mongoCollection.Insert(p);
//BsonDocument 插入
BsonDocument b = new BsonDocument();
b.Add("Uid", 125);
b.Add("Name", "xixiBson");
b.Add("PassWord", "333333");
mongoCollection.Insert(b);
Console.ReadLine();
}
}
class Person {
public int Uid;
public string Name;
public string PassWord;
}
}
结果:
都是上述配置写的,程序会自动建立对应的库和集合。
下面的操作不上完整代码了:
/*---------------------------------------------
* sql : SELECT * FROM table
*---------------------------------------------
*/
MongoCursor<Person> p = mongoCollection.FindAllAs<Person>();
/*---------------------------------------------
* sql : SELECT * FROM table WHERE Uid > 10 AND Uid < 20
*---------------------------------------------
*/
QueryDocument query = new QueryDocument();
BsonDocument b = new BsonDocument();
b.Add("$gt", 10);
b.Add("$lt", 20);
query.Add("Uid", b);
MongoCursor<Person> m = mongoCollection.FindAs<Person>(query);
/*-----------------------------------------------
* sql : SELECT COUNT(*) FROM table WHERE Uid > 10 AND Uid < 20
*-----------------------------------------------
*/
long c = mongoCollection.Count(query);
/*-----------------------------------------------
* sql : SELECT Name FROM table WHERE Uid > 10 AND Uid < 20
*-----------------------------------------------
*/
QueryDocument query = new QueryDocument();
BsonDocument b = new BsonDocument();
b.Add("$gt", 10);
b.Add("$lt", 20);
query.Add("Uid", b);
FieldsDocument f = new FieldsDocument();
f.Add("Name", 1);
MongoCursor<Person> m = mongoCollection.FindAs<Person>(query).SetFields(f);
/*-----------------------------------------------
* sql : SELECT * FROM table ORDER BY Uid DESC LIMIT 10,10
*-----------------------------------------------
*/
QueryDocument query = new QueryDocument();
SortByDocument s = new SortByDocument();
s.Add("Uid", -1);//-1=DESC
MongoCursor<Person> m = mongoCollection.FindAllAs<Person>().SetSortOrder(s).SetSkip(10).SetLimit(10);
MongoDB 之 Find查询
MongoDB Find查询 1 public class News
{
public int _id { get; set; }
public int count { get; set; }
public string news { get; set; }
public DateTime time { get; set; }
}
MongoCursor<BsonDocument> allDoc = coll.FindAllAs<BsonDocument>();
BsonDocument doc = allDoc.First(); //BsonDocument类型参数
MongoCursor<News> allNews = coll.FindAllAs<News>();
News aNew = allNews.First(); //News类型参数
News firstNews = coll.FindOneAs<News>(); //查找第一个文档
QueryDocument query = new QueryDocument(); //定义查询文档
query.Add("_id", 10001);
query.Add("count", 1);
MongoCursor<News> qNews = coll.FindAs<News>(query);
BsonDocument bd = new BsonDocument();//定义查询文档 count>2 and count<=4
bd.Add("$gt", 2);
bd.Add("$lte", 4);
QueryDocument query_a = new QueryDocument();
query_a.Add("count",bd);
FieldsDocument fd = new FieldsDocument();
fd.Add("_id", 0);
fd.Add("count", 1);
fd.Add("time", 1);
MongoCursor<News> mNewss = coll.FindAs<News>(query_a).SetFields(fd);//只返回count和time
var time = BsonDateTime.Create("2011/9/5 23:26:00");
BsonDocument db_t = new BsonDocument();
db_t.Add("$gt", time);
QueryDocument qd_3 = new QueryDocument();
qd_3.Add("time", db_t);
MongoCursor<News> mNews = coll.FindAs<News>(qd_3);//
2 {
3 public int _id { get; set; }
4 public int count { get; set; }
5 public string news { get; set; }
6 public DateTime time { get; set; }
7 }
8
9 MongoCursor<BsonDocument> allDoc = coll.FindAllAs<BsonDocument>();
10 BsonDocument doc = allDoc.First(); //BsonDocument类型参数
11
12 MongoCursor<News> allNews = coll.FindAllAs<News>();
13 News aNew = allNews.First(); //News类型参数
14
15 News firstNews = coll.FindOneAs<News>(); //查找第一个文档
16
17 QueryDocument query = new QueryDocument(); //定义查询文档
18 query.Add("_id", 10001);
19 query.Add("count", 1);
20 MongoCursor<News> qNews = coll.FindAs<News>(query);
21
22
23 BsonDocument bd = new BsonDocument();//定义查询文档 count>2 and count<=4
24 bd.Add("$gt", 2);
25 bd.Add("$lte", 4);
26 QueryDocument query_a = new QueryDocument();
27 query_a.Add("count",bd);
28
29 FieldsDocument fd = new FieldsDocument();
30 fd.Add("_id", 0);
31 fd.Add("count", 1);
32 fd.Add("time", 1);
33
34 MongoCursor<News> mNewss = coll.FindAs<News>(query_a).SetFields(fd);//只返回count和time
35
36 var time = BsonDateTime.Create("2011/9/5 23:26:00");
37 BsonDocument db_t = new BsonDocument();
38 db_t.Add("$gt", time);
39 QueryDocument qd_3 = new QueryDocument();
40 qd_3.Add("time", db_t);
41
42 MongoCursor<News> mNews = coll.FindAs<News>(qd_3);//
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.