本文整理汇总了C#中MongoDB.Driver.Document.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Document.Add方法的具体用法?C# Document.Add怎么用?C# Document.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoDB.Driver.Document
的用法示例。
在下文中一共展示了Document.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestFromDocument
public void TestFromDocument()
{
Document doc = new Document();
doc.Add("string", "test");
doc.Add("int", 1);
BsonDocument bdoc = BsonConvert.From(doc);
Assert.AreEqual(2,bdoc.Count);
/*doc.Add("date", DateTime.MinValue);
Assert.Throws<ArgumentOutOfRangeException>(
delegate {BsonConvert.From(doc);});*/ //Not in nUnit 2.4.
}
示例2: ToDocument
/// <summary>
/// Converts the dictionary to document.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <param name="document">The document.</param>
public static Document ToDocument(this IDictionary<string, object> dictionary)
{
var document = new Document();
foreach (var kvp in dictionary)
{
if (kvp.Value is IDictionary<string, object>)
{
var subDocument = ((IDictionary<string, object>)kvp.Value).ToDocument();
document.Add(kvp.Key, subDocument);
}
else
document.Add(kvp.Key, kvp.Value);
}
return document;
}
示例3: TestKeyOrderPreservedOnRemove
public void TestKeyOrderPreservedOnRemove()
{
Document d = new Document();
d["one"] = 1;
d["onepointfive"] = 1.5;
d.Add("two", 2);
d.Add("two.5", 2.5);
d.Remove("two.5");
d["three"] = 3;
d.Remove("onepointfive");
int cnt = 1;
foreach(String key in d.Keys){
Assert.AreEqual(cnt, d[key]);
cnt++;
}
}
示例4: CreateNut
/// <summary>
/// Create a Nut for a specific application account and in a specific configuration container.
/// </summary>
/// <param name="account">The application account we are creating the nut for.</param>
/// <param name="container">The container we are inserting the nut into (e.g. connectionstrings, endpoints, appconfig, etc).</param>
/// <param name="nut">Busta nut.</param>
/// <returns>True if the nut was added and false if it already exists, and therefore was not added/updated.</returns>
public static bool CreateNut(string account, string container, Nut nut)
{
bool nutAdded = false;
Mongo mongo = new Mongo();
mongo.Connect();
var db = mongo.GetDatabase(WellKnownDb.AppConfiguration);
var collection = db.GetCollection(container);
var doc = new Document();
doc["account"] = account;
doc["name"] = nut.Key;
doc["value"] = nut.Value;
if (nut.Properties != null)
{
foreach (var k in nut.Properties.Keys)
{
doc.Add(k, nut.Properties[k]);
}
}
if (collection.FindOne(doc) == null)
{
collection.Insert(doc);
nutAdded = true;
}
return nutAdded;
}
示例5: ReadElement
public void ReadElement(Document doc)
{
sbyte typeNum = (sbyte)reader.ReadByte ();
position++;
String key = ReadString ();
Object element = ReadElementType(typeNum);
doc.Add (key, element);
}
示例6: generateQueryMessage
protected QueryMessage generateQueryMessage()
{
Document qdoc = new Document();
qdoc.Add("listDatabases", 1.0);
//QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces");
QueryMessage qmsg = new QueryMessage(qdoc,"admin.$cmd");
qmsg.NumberToReturn = -1;
return qmsg;
}
示例7: DeletePost
/// <summary>
/// Deletes a post from the data store.
/// </summary>
public override void DeletePost(Post post)
{
using (var mongo = new MongoDbWr())
{
var coll = mongo.BlogDB.GetCollection("posts");
Document spec = new Document();
spec.Add("id", post.Id);
coll.Delete(spec);
}
}
示例8: TestCopyToCopiesAndOverwritesKeys
public void TestCopyToCopiesAndOverwritesKeys()
{
Document d = new Document();
Document dest = new Document();
dest["two"] = 200;
d["one"] = 1;
d.Add("two", 2);
d["three"] = 3;
d.CopyTo(dest);
Assert.AreEqual(2, dest["two"]);
}
示例9: TestKeyOrderIsPreserved
public void TestKeyOrderIsPreserved()
{
Document d = new Document();
d["one"] = 1;
d.Add("two", 2);
d["three"] = 3;
int cnt = 1;
foreach(String key in d.Keys){
Assert.AreEqual(cnt, d[key]);
cnt++;
}
}
示例10: TestClearRemovesAll
public void TestClearRemovesAll()
{
Document d = new Document();
d["one"] = 1;
d.Add("two", 2);
d["three"] = 3;
Assert.AreEqual(3,d.Count);
d.Clear();
Assert.AreEqual(0, d.Count);
Assert.IsNull(d["one"]);
Assert.IsFalse(d.Contains("one"));
}
示例11: Authenticate
public bool Authenticate(string username, string password)
{
Document nonceResult = this.SendCommand("getnonce");
String nonce = (String)nonceResult["nonce"];
if (nonce == null){
throw new MongoException("Error retrieving nonce", null);
}
else {
string pwd = Database.Hash(username + ":mongo:" + password);
Document auth = new Document();
auth.Add("authenticate", 1.0);
auth.Add("user", username);
auth.Add("nonce", nonce);
auth.Add("key", Database.Hash(nonce + username + pwd));
try{
this.SendCommand(auth);
return true;
}catch(MongoCommandException){
return false;
}
}
}
示例12: TestCopyToCopiesAndPreservesKeyOrderToEmptyDoc
public void TestCopyToCopiesAndPreservesKeyOrderToEmptyDoc()
{
Document d = new Document();
Document dest = new Document();
d["one"] = 1;
d.Add("two", 2);
d["three"] = 3;
d.CopyTo(dest);
int cnt = 1;
foreach(String key in dest.Keys){
Assert.AreEqual(cnt, d[key]);
cnt++;
}
}
示例13: TestRoundTrip
public void TestRoundTrip()
{
Document idoc = new Document();
idoc.Add("b",new Binary(new byte[]{(byte)1,(byte)2}));
MemoryStream stream = new MemoryStream();
BsonWriter writer = new BsonWriter(stream);
writer.Write(idoc);
stream.Seek(0,SeekOrigin.Begin);
BsonReader reader = new BsonReader(stream);
Document odoc = reader.Read();
Assert.AreEqual(idoc.ToString(), odoc.ToString());
}
示例14: BuildFromObject
public static Document BuildFromObject(object spec)
{
Document result = new Document();
if (spec != null)
{
foreach (var property in spec.GetType().GetProperties())
{
if (property.CanRead)
{
object value = property.GetValue(spec, null);
result.Add(property.Name, value ?? MongoDBNull.Value);
}
}
}
return result;
}
示例15: Set
public static void Set(string key, string val)
{
var doc = new Document { { "key", key } };
var res = AdminParametersCollection.FindOne(doc);
if (res == null)
{
doc.Add("value", val);
AdminParametersCollection.Insert(doc);
}
else
{
res["value"] = val;
AdminParametersCollection.Update(res);
}
}