MongoDB+;C#:在文档中查询
似乎我不明白如何从文档中的集合中获取值。我正在C#中使用mongoDB 这是我的密码:MongoDB+;C#:在文档中查询,c#,mongodb,C#,Mongodb,似乎我不明白如何从文档中的集合中获取值。我正在C#中使用mongoDB 这是我的密码: var jimi = new Document(); jimi["Firstname"] = "Jimi"; jimi["Lastname"] = "James"; jimi["Pets"] = new[] { new Document().Append("Type", "Cat").Append("Name", "Fluffy"), new Document().Append("Type"
var jimi = new Document();
jimi["Firstname"] = "Jimi";
jimi["Lastname"] = "James";
jimi["Pets"] = new[]
{
new Document().Append("Type", "Cat").Append("Name", "Fluffy"),
new Document().Append("Type", "Dog").Append("Name", "Barky"),
new Document().Append("Type", "Gorilla").Append("Name", "Bananas"),
};
test.Insert(jimi);
var query = new Document().Append("Pets.Type","Cat");
因此,我的查询将查找宠物猫。但是我不知道怎样才能知道我的猫的名字。我试了一些方法,但大部分都能把整个文件拿回来
提前感谢,
Pickels这并不像我希望的那样优雅,因为我自己还在学习MongoDB,但它确实向您展示了一种获得所需房产的方法
[TestFixture]
public class When_working_with_nested_documents
{
[Test]
public void Should_be_able_to_fetch_properties_of_nested_objects()
{
var mongo = new Mongo();
mongo.Connect();
var db = mongo.getDB("tests");
var people = db.GetCollection("people");
var jimi = new Document();
jimi["Firstname"] = "Jimi";
jimi["Lastname"] = "James";
jimi["Pets"] = new[]
{
new Document().Append("Type", "Cat").Append("Name", "Fluffy"),
new Document().Append("Type", "Dog").Append("Name", "Barky"),
new Document().Append("Type", "Gorilla").Append("Name", "Bananas"),
};
people.Insert(jimi);
var query = new Document();
query["Pets.Type"] = "Cat";
var personResult = people.FindOne(query);
Assert.IsNotNull(personResult);
var petsResult = (Document[])personResult["Pets"];
var pet = petsResult.FindOne("Type", "Cat");
Assert.IsNotNull(pet);
Assert.AreEqual("Fluffy", pet["Name"]);
}
}
public static class DocumentExtensions
{
public static Document FindOne(this Document[] documents, string key, string value)
{
foreach(var document in documents)
{
var v = document[key];
if (v != null && v.Equals(value))
{
return document;
}
}
return null;
}
}
这并不像我希望的那样优雅,因为我自己还在学习MongoDB,但它确实向您展示了一种获得所需财产的方法
[TestFixture]
public class When_working_with_nested_documents
{
[Test]
public void Should_be_able_to_fetch_properties_of_nested_objects()
{
var mongo = new Mongo();
mongo.Connect();
var db = mongo.getDB("tests");
var people = db.GetCollection("people");
var jimi = new Document();
jimi["Firstname"] = "Jimi";
jimi["Lastname"] = "James";
jimi["Pets"] = new[]
{
new Document().Append("Type", "Cat").Append("Name", "Fluffy"),
new Document().Append("Type", "Dog").Append("Name", "Barky"),
new Document().Append("Type", "Gorilla").Append("Name", "Bananas"),
};
people.Insert(jimi);
var query = new Document();
query["Pets.Type"] = "Cat";
var personResult = people.FindOne(query);
Assert.IsNotNull(personResult);
var petsResult = (Document[])personResult["Pets"];
var pet = petsResult.FindOne("Type", "Cat");
Assert.IsNotNull(pet);
Assert.AreEqual("Fluffy", pet["Name"]);
}
}
public static class DocumentExtensions
{
public static Document FindOne(this Document[] documents, string key, string value)
{
foreach(var document in documents)
{
var v = document[key];
if (v != null && v.Equals(value))
{
return document;
}
}
return null;
}
}
啊,所以你必须自己处理文档内部的查询。我错过了一些非常明显的东西。非常感谢,所以您必须自己处理文档内部的查询。我错过了一些非常明显的东西。非常感谢签出=>将实体转换为文档。签出=>将实体转换为文档。