MongoDBRef 如何编写查询
我正在使用 MongoDB 官方驱动程序 (10Gen)。我无法查询 MonogoDBRef 属性。我有以下类:
public class UserData
{
private ObjectId id;
public ObjectId _id
{
get { return id; }
set { id = value; }
}
[BsonElement("Mail")]
public string Email { get; set; }
public string Name{ get; set; }
}
public class UserSettings
{
private ObjectId id;
public ObjectId _id
{
get { return id; }
set { id = value; }
}
[BsonElement("usr")]
public MongoDBRef User { get; set; }
public List<SettingsUser> Settings{ get; set; }
}
我想进行一个查询,通过 UserData 获取该用户的 UserSettings。 我尝试了以下方法,但它不起作用:
var colletion = db.GetCollection<UserSettings>("UsrSettings");
collection.Find(Query.EQ("usr", usr._id);
我也尝试了这个:
collection.Find(Query.EQ("usr", new MongoDBRef("UsrSettings", usr._id));
但它无法编译,因为 MongoDBRef 不是 BsonValue。
另一次尝试:
collection.FindOne(Query.EQ("usr.$id", User._id));
我得到了异常:意外的元素“$ref”。
有什么想法吗?或解决方法?谢谢!
I'm working with the MongoDB official driver (10Gen). And I cannot query a MonogoDBRef propertie. I have the following classes:
public class UserData
{
private ObjectId id;
public ObjectId _id
{
get { return id; }
set { id = value; }
}
[BsonElement("Mail")]
public string Email { get; set; }
public string Name{ get; set; }
}
public class UserSettings
{
private ObjectId id;
public ObjectId _id
{
get { return id; }
set { id = value; }
}
[BsonElement("usr")]
public MongoDBRef User { get; set; }
public List<SettingsUser> Settings{ get; set; }
}
I want to make a query that having the UserData I fetch the UserSettings of that user.
I try the following but it does not work:
var colletion = db.GetCollection<UserSettings>("UsrSettings");
collection.Find(Query.EQ("usr", usr._id);
also I tried this:
collection.Find(Query.EQ("usr", new MongoDBRef("UsrSettings", usr._id));
But it does not compile because MongoDBRef is not a BsonValue.
Another try:
collection.FindOne(Query.EQ("usr.$id", User._id));
And I get the exception: Unexpected element '$ref'.
Any idea? or workaround? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Sridhar 在这里回答了我的问题:https://groups.google.com /forum/#!msg/mongodb-user/Tip9AQa_1TE/YAgflwJa3tAJ
下面应该给你你想要的(注意我使用的是1.1
driver)
这里userdata是存储用户数据的集合的名称。
话虽如此,如果 UserSettings 集合中的所有文档
始终仅引用 UserData 集合中的文档,然后您
应该只使用指定的手动参考
http://www.mongodb.org/display/DOCS/Database+References #DatabaseReferences-DBRef。
DBRefs 对于文档在单个文件中的情况很有用
集合可以引用多个其他集合中的文档。
Sridhar answered my question here: https://groups.google.com/forum/#!msg/mongodb-user/Tip9AQa_1TE/YAgflwJa3tAJ
The following should give you what you want (note I am using the 1.1
driver)
Here userdata is the name of the collection that stores user data.
Having said that if all documents in the UserSettings collection
always refer to only documents from the UserData collection then you
should just use a manual reference as specified in
http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef.
DBRefs are useful for the scenario where documents in a single
collection can reference documents from multiple other collections.