mongoDB学习记录

1.启动服务报错100

配置的db目录中有两个文件一个mongod.lock和storage.bson
删除mongod.lock,如果服务错误代码100还不能解决删掉storage.bson再启动

2.时间

ISODate("2018-05-26 12:14:30.800")

3.导入数据集

.\mongoimport.exe --db DB01 --collection machine_95 --file 'C:\data.json'

4.调整内存

db.adminCommand({setParameter:1, internalQueryExecMaxBlockingSortBytes:335544320})

 5.模糊查询

db.machine_95.find({mod:{$regex:'3'}})

 6.sum

 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])

 7.使用ObjectID

var o2 = Coll.Find(f => f._id==new BsonObjectId(new ObjectId("5b51912427a0a9051c05cde3"))).ToList();

 8.重命名数据库

db.copyDatabase('old_name', 'new_name');

 9.查询两个字段不相等

{$where:"this.Type==this.Spindle"}

 10. linq

//使用AsQueryable
       var data = db.GetCollection<TESTModel>("test").AsQueryable().Where((r => r.MyProperty == "test"));

11. id字段

        public ObjectId Id { get; set; }

12. 自定义ID

 [BsonId]
        public string Id { get; set; }

13. 连接字符串

mongodb://user:test@127.0.0.1:27017

 14. nuget

官方包 MongoDB.Driver

15.连接字符串

mongodb://user:password@127.0.0.1:27017

//扩展参数可以配置主从、超时等

 

16.动态授权 

string host;
int port;
string db; //授权数据库 - 一般为admin
string username;
string password;
TimeSpan? timeSpan = null;
int maxConnectionPoolSize = 2000;
var config = new MongoClientSettings
            {
                Server = new MongoServerAddress(host, port),
                MaxConnectionPoolSize = maxConnectionPoolSize
            };

            if (timeSpan != null)
            {
                config.ConnectTimeout = timeSpan.Value;
            }

            config.Credential = MongoCredential.CreateCredential(db, username, password);

            var mongoClient = new MongoClient(config);

 17.更新字段

var update= Builders<TestModel>.Update.Set("diffName", "888");
Collection.UpdateOne(f=>f.Name=="test", update);

 

 

 

posted @ 2018-05-29 18:24  Hey,Coder!  阅读(248)  评论(0编辑  收藏  举报