Mongodb Notes

Mongodb Notes

HashFlare

Install:

1.download zip from mongodb ,suggest you use latest version, extract to maybe ‘d:\mongodb’

2.set System variables Path add ‘d:\mong\bin’

3.create folder for dbpath d:\mongodb\data

in the case you come accross ‘the program can’t start because ssleay32.dll is missing from your computer’,

you can fix it or using install files instead of manually install.

mongodb1

type http://localhost:27017/ in browser, will show

It looks like you are trying to access MongoDB over HTTP on the native driver port.
that is success.
(tips:
if later you encounter "Unable to connect to server localhost:27017: Invalid credential for ..:
please check whether port 27017 is blocked by firewall,can run cmd:
netsh advfirewall firewall add rule name="MongoDB 27017" dir=in action=allow protocol=TCP localport=27017
)

4.further step, if you want to make it a service, prepare mongd.cfg files:

logpath = c:\mongodb\log\log.txt
dbpath = c:\mongodb\data
directoryperdb = true
logappend = true
auth = false
setParameter = enableLocalhostAuthBypass=0
master = true
oplogSize = 500

then run

mongod –config c:\mongodb\mongd.cfg –install

to set up service ( to remove: replace ‘install’ with ‘remove’)

IMPORTANT

If you are running any edition of Windows Server 2008 R2 or Windows 7, please install a hotfix to resolve an issue with memory mapped files on Windows.

5.connect mongodb

a. directly connect: mongo localhost/dbname -u*** -p***

b.use mongo cmd, will connect to ‘test’ automatically, db cmd will show current database, show dbs will show all dbs, if got ‘listDatabases failed:{ “ok” : 0, “errmsg” : “unauthorized” } at src/mongo/shell/mongo.js:46’, according to the documentation, the user must be granted the clusterAdmin role for the listDatabases privilege

6.auth

prepare admin.js:

db = db.getSiblingDB("admin");
db.createUser({user:"test", pwd:"123456",roles:[{"role":"root","db":"admin"}]});//old version: db.addUser("sa", "123456");
db.auth("sa", "123456");

run mongo localhost/admin c:\mongodb\admin.js (before doing this, make sure mongodb is running)

IMPORTANT

1. You may encounter “I CONTROL Hotfix KB2731284 or later update is not installed, will zero-out data files”

then stop mongodb service, and modify mongod.cfg set auth=true, it will work

next time if you wanna run js,must do like this:

mongo -usa -p123456 localhost/admin c:\mongodb\mongo-newdb.js:

print(“===== newdb =====”);
db = db.getSiblingDB(“dbname”);
db.createUser({user:”dev”, pwd:”123456″,roles:[{“role”:”dbOwner”,”db”:”dbname”}]});//old version: db.addUser(“dev”, “123456”);
db.auth(“dev”, “123456”);

print(“— insert data —“);
db.Client.insert({ “_id” : “123”, “Name” : “lyhistory”, “Secret” : “lyhistory”, “Callback” : “https://lyhistory.com”, “ClientType” : 1 });

 2. Version Issues

The schema of db.system.users in mongodb 2.4 and 2.6 are different which caused you fail to create new user.

http://stackoverflow.com/questions/23468137/how-to-add-authentication-to-mongodb-2-6

更改认证模式 MONGODB-CR / SCRAM-SHAR-1

http://www.xlgps.com/article/441478.html

Cmds:

c:\mongodb\bin>mongo localhost/mydb -udev -p123456
MongoDB shell version: 2.6.8
connecting to: localhost/mydb

> show tables
Friend
Message
system.indexes
> db.Friend.find({“_id”:1}).pretty();
{
“_id” : 1,
“Friends” : [
{
{
“UserId” : 2,
“Name” : “Test 1”,
“Avatar” : null,
“IsOnline” : false,
“State” : 1,
“NameType” : “3”
},
{
“UserId” : 3,
“Name” : “Test 2”,
“Avatar” : null,
“IsOnline” : false,
“State” : 1,
“NameType” : “3”
}
]
}
>

Describe Schema

> var schema=db.ChatUser.findOne();
> for(var key in schema){print(key);}
_id
UserName
Gender

db.TableName.drop()

remove a deprecated filed(‘column’)

db.collection.update(
{ ‘<field>’: { ‘$exists’: true } },  // Query
{ ‘$unset’: { ‘<field>’: true  } },  // Update
false, true                          // Upsert, Multi
)

Update for MongoDB 2.2+:
You can now provide a JSON object instead of positional arguments for upsert and multi.
db.collection.update(
{ ‘<field>’: { ‘$exists’: true } },  // Query
{ ‘$unset’: { ‘<field>’: true  } },  // Update
{ ‘multi’: true }                    // Options
)

> db.ChatUser.update({‘IsFriend’:{‘$exists’:true}},{‘$unset’:{‘IsFriend’:true}},false,true);
WriteResult({ “nMatched” : 5, “nUpserted” : 0, “nModified” : 5 })

Add new fields:

db.your_collection.update({},{$set : {“new_field”:1}},false,true)  –> 1 here represents double type,if you want to use as string, replace it with “1”

Migrate mongodb database from localhost to remote servers

https://docs.mongodb.org/manual/tutorial/backup-and-restore-tools/

mongo export import

MongoDB import and export example

mongodump mongorestore

C#

1.MongoDB.Bson.Serialization

BsonClassMap.RegisterClassMap<Friend>(x =>
{
x.AutoMap();
x.SetIdMember(x.GetMemberMap(m => m.UserId));
x.IdMemberMap.SetRepresentation(BsonType.String);
x.GetMemberMap(m => m.UserId).SetIgnoreIfNull(true);
});
BsonClassMap.RegisterClassMap<Message>(x =>
{
x.AutoMap();
x.IdMemberMap.SetRepresentation(BsonType.ObjectId);
x.GetMemberMap(m => m.Id).SetIgnoreIfNull(true);
});

Get _id of an inserted document in MongoDB?

simply add this filed to your own class

[BsonId]
public ObjectId ID{get;set;}

2.Update Mongodb

var updatebuidler = new List<UpdateBuilder>();
updatebuidler.Add(MongoDB.Driver.Builders.Update.Set(“<COLUMN>”, new BsonArray(<COLUMN VALUE>)));
this.Update(Query.EQ(“_id”, ObjectId.Parse(id)),MongoDB.Driver.Builders.Update.Combine(updatebuidler));

3.UTC Date

list.ToList().ForEach(x =>
{
if (x.UpdateDate != null)
{
x.UpdateDate = x.UpdateDate .ToLocalTime();
}
});

 

pagination

http://blog.mongodirector.com/fast-paging-with-mongodb/

http://stackoverflow.com/questions/9703319/mongodb-ranged-pagination

 

MongoDB-CR Authentication failed http://stackoverflow.com/questions/29006887/mongodb-cr-authentication-failed

想使用 MongoDB ,你应该了解这8个方面! https://yq.aliyun.com/articles/7245

阿里云MongoDB技术内幕 https://yq.aliyun.com/topic/58?utm_campaign=wenzhang&utm_medium=article&utm_source=QQ-qun&utm_content=m_7769

如何把数据迁移到阿里云云MongoDb www.jianshu.com/p/d8a64bc90605

【Mongodb】用户和认证 权限总结  https://yq.aliyun.com/articles/27952

mongdb3.0用户验证问题  www.bbsmax.com/A/B0zq2eg8Jv/

http://canonind.blog.51cto.com/8239025/1843220

driver: https://pecl.php.net/package/mongodb

GUI

robomongo

http://docs.mongodb.org/getting-started/csharp/

Using MongoDB with the Official C# Driver

 

repository:

https://github.com/mongodb

友荐云推荐