English 中文(简体)
MongoDB - How to query with multi and conditions for Telephone number using FilterDefinition
原标题:MongoDB - How to query with multiple AND conditions for phone numbers using FilterDefinition

我有一份名为“申请人”的MongoDB藏书,其中载有关于申请人的信息,包括申请人的姓、类型和形形形色的电话号码,如“家庭”、“个人”和“办公室”。

Here s a sample JSON representation of an "Applicant" document:

{
  "_id": ObjectId("61791ecf2abf206fd5e5e322"),
  "LastName": "Smith",
  "Type": "P",
  "PhoneNumbers": [
    { "Type": "home", "Value": "111-111-1111" },
    { "Type": "cell", "Value": "222-222-2222" },
    { "Type": "office", "Value": "333-333-3333" }
  ]
}

现在,我需要做一个问询,找到有具体电话号码的申请人,例如“家”和“大”电话号码。 我正在使用MongoDB C#司机,希望使用<代码>FilterDefinition。

我曾尝试过使用<代码>ElemMatch的多种条件方法,但似乎没有按预期工作。 我目前试图:

var filterBuilder = Builders<Applicant>.Filter;
var homePhoneFilter = filterBuilder.Eq("PhoneNumbers.Type", "home") & filterBuilder.Eq("PhoneNumbers.Value", "111-111-1111");
var cellPhoneFilter = filterBuilder.Eq("PhoneNumbers.Type", "cell") & filterBuilder.Eq("PhoneNumbers.Value", "222-222-2222");
var queryFilter = homePhoneFilter & cellPhoneFilter;

不幸的是,这只字不提任何结果,我怀疑,如果使用<条码>和<>条码>,将和<条码>与<条码>挂钩,可能不是正确的做法。

Rendered Query:

以下是预期的提问:

{
  "PhoneNumbers": {
    "$elemMatch": { "$and": [ { "Type": "home" }, { "Value": "111-111-1111" } ], "$and": [ { "Type": "cell" }, { "Value": "222-222-2222" } ] }
  }
}

Any guidance on how to correctly implement multiple AND conditions using FilterDefinition in C# for my MongoDB query would be highly appreciated.

事先感谢你的帮助!

问题回答




相关问题
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. ...

热门标签