forked from samus/mongodb-csharp
/
DatabaseMetaData.cs
76 lines (62 loc) · 2.48 KB
/
DatabaseMetaData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
namespace MongoDB.Driver
{
/// <summary>
/// Administration of metadata for a database.
/// </summary>
public class DatabaseMetaData
{
private Connection connection;
private string name;
private Database db;
public DatabaseMetaData(string name, Connection conn){
this.connection = conn;
this.name = name;
this.db = new Database(conn, name);
}
public Collection CreateCollection(String name){
return this.CreateCollection(name,null);
}
public Collection CreateCollection(String name, Document options){
Document cmd = new Document();
cmd.Append("create", name).Update(options);
db.SendCommand(cmd);
return new Collection(name, connection, this.name);
}
public Boolean DropCollection(Collection col){
return this.DropCollection(col.Name);
}
public Boolean DropCollection(String name){
Document result = db.SendCommand(new Document().Append("drop",name));
return result.Contains("ok") && ((double)result["ok"] == 1);
}
public Boolean DropDatabase(){
throw new NotImplementedException();
}
public void AddUser(string username, string password){
IMongoCollection users = db["system.users"];
string pwd = Database.Hash(username + ":mongo:" + password);
Document user = new Document().Append("user", username).Append("pwd", pwd);
if (FindUser(username) != null){
throw new MongoException("A user with the name " + username + " already exists in this database.", null);
}
else{
users.Insert(user);
}
}
public void RemoveUser(string username){
IMongoCollection users = db["system.users"];
users.Delete(new Document().Append("user", username));
}
public ICursor ListUsers(){
IMongoCollection users = db["system.users"];
return users.FindAll();
}
public Document FindUser(string username){
return FindUser(new Document().Append("user",username));
}
public Document FindUser(Document spec){
return db["system.users"].FindOne(spec);
}
}
}