C# 驱动操作 (对照命令行 )
3 、支持更新数据
var query = Query.And(Query.EQ("Number", gcommentInfo.Number),
Query.EQ("IsHotting", 1));
var update = Update.Set("Id", gcommentInfo.Id)
.Set("NewsId", gcommentInfo.NewsId)
.Set("ChnId", gcommentInfo.ChnId);
var info = mongoGCommentInfos.FindOne(query);
if (info != null)
mongoGCommentInfos.Update(query, update);
13.
MongoDB 命令行操作
普通操作
4 、查询:
4.1遍历集
> var cursor = db.things.find();
> while (cursor.hasNext()) { print(tojson(cursor.next())); }
4.2 方法 2
> db.things.find().forEach( function(x){print(tojson(x));});
4.3 、获取结果集
> var cursor = db.things.find();
> print (tojson(cursor[4]));
> var arr = db.things.find().toArray();
> arr[5];
14.
C# 驱动操作 (对照命令行 )
FindAll 用法 :
MongoDB.Driver.MongoCursor<HeadLineInfo>
mongoHeadLineInfos = new MongoHelper().GetCollection<HeadLineInfo>();
var mongoHeadLineInfoList = mongoHeadLineInfos.FindAll();
15.
MongoDB 命令行操作
普通操作
5 、条件查询:
>db.things.find({name:"mongo"}).forEach(function(x) { print(tojson(x));});
等价于:
SQL : SELECT * FROM things WHERE name="mongo"
>db.things.find({x:4}, {j:true}).forEach(function(x) { print(tojson(x));});
等价于:
SQL : SELECT j FROM things WHERE x=4
C# 驱动操作 (对照命令行 )
g. 索引
添加索引:
var headLineInfos = new
MongoHelper().GetCollection<HeadLineInfo>();
headLineInfos.EnsureIndex(IndexKeys.Descending("NewsId"));
headLineInfos.EnsureIndex(IndexKeys.Descending(“ChannelType”,
“SubmitTime”));( 支持复合索引 )
移除索引:
headLineInfos.DropIndex(IndexKeys.Descending("IsVertical"));
h. findOne 用法
var query = Query.EQ("_id", MongoDB.Bson.BsonString.Create(KeyId));
var info = newsinfos.FindOne(query);