English 中文(简体)
MongoDBRef 如何撰写询问
原标题:MongoDBRef how to write a query

Im与MongoDB官方司机(10人)合作。 我不能质疑莫奥果非行。 我有以下班子:

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);

我也试图这样做:

collection.Find(Query.EQ("usr", new MongoDBRef("UsrSettings", usr._id));

但由于MongoDBRef不是BsonValue,它没有汇编。

另一项尝试:

collection.FindOne(Query.EQ("usr.$id", User._id));

我例外: 未预见的元件。

任何想法? 还是工作? 感谢!

最佳回答

Sridhar回答了我在此提出的问题:https://groups.google.com/forum/#!msg/mongodb-user/Tip9A Qa_1TE/YAgflwJa3tAJ

The following should give you what you want (note I am using the 1.1 driver)

var refDocument = new BsonDocument { 
            {"$ref", "userdata"}, 
            {"$id", usr._id} 
        }; 
var query = Query.EQ("usr", refDocument); 
var result = userDataCollection.FindOne(query); 

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.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签