selecting fields in a document after applying filter on embedded document
up vote
1
down vote
favorite
Can you let me know the C# equivalent of below query please?
db.RolesNPerm.find(
{ "Roles.Name":{$in:[ "PO","role1","TO"] }},
{ _id: 1, ParentId: 1 }
)
I am trying to select the fields(_id
and ParentId
) in the record if the Name
in the embedded document(Roles
) matches any of the value in the list.
Here is my MongoDB document
{
"_id" : "1",
"ParentId" : "par1",
"Roles" : [
{
"Name" : "PO",
"_id" : "5bc08ee1f12541c3aaa03084"
}
]
}
Below is the result of my query as the Role.Name matches "PO" in the list of names.
{
"_id" : "1",
"ParentId" : "par1"
}
c# mongodb nested
add a comment |
up vote
1
down vote
favorite
Can you let me know the C# equivalent of below query please?
db.RolesNPerm.find(
{ "Roles.Name":{$in:[ "PO","role1","TO"] }},
{ _id: 1, ParentId: 1 }
)
I am trying to select the fields(_id
and ParentId
) in the record if the Name
in the embedded document(Roles
) matches any of the value in the list.
Here is my MongoDB document
{
"_id" : "1",
"ParentId" : "par1",
"Roles" : [
{
"Name" : "PO",
"_id" : "5bc08ee1f12541c3aaa03084"
}
]
}
Below is the result of my query as the Role.Name matches "PO" in the list of names.
{
"_id" : "1",
"ParentId" : "par1"
}
c# mongodb nested
What have you tried? SO is not code conversion service.
– Reniuz
Nov 21 at 14:38
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Can you let me know the C# equivalent of below query please?
db.RolesNPerm.find(
{ "Roles.Name":{$in:[ "PO","role1","TO"] }},
{ _id: 1, ParentId: 1 }
)
I am trying to select the fields(_id
and ParentId
) in the record if the Name
in the embedded document(Roles
) matches any of the value in the list.
Here is my MongoDB document
{
"_id" : "1",
"ParentId" : "par1",
"Roles" : [
{
"Name" : "PO",
"_id" : "5bc08ee1f12541c3aaa03084"
}
]
}
Below is the result of my query as the Role.Name matches "PO" in the list of names.
{
"_id" : "1",
"ParentId" : "par1"
}
c# mongodb nested
Can you let me know the C# equivalent of below query please?
db.RolesNPerm.find(
{ "Roles.Name":{$in:[ "PO","role1","TO"] }},
{ _id: 1, ParentId: 1 }
)
I am trying to select the fields(_id
and ParentId
) in the record if the Name
in the embedded document(Roles
) matches any of the value in the list.
Here is my MongoDB document
{
"_id" : "1",
"ParentId" : "par1",
"Roles" : [
{
"Name" : "PO",
"_id" : "5bc08ee1f12541c3aaa03084"
}
]
}
Below is the result of my query as the Role.Name matches "PO" in the list of names.
{
"_id" : "1",
"ParentId" : "par1"
}
c# mongodb nested
c# mongodb nested
edited Nov 21 at 18:38
mickl
10.4k51435
10.4k51435
asked Nov 21 at 14:23
user641247
154
154
What have you tried? SO is not code conversion service.
– Reniuz
Nov 21 at 14:38
add a comment |
What have you tried? SO is not code conversion service.
– Reniuz
Nov 21 at 14:38
What have you tried? SO is not code conversion service.
– Reniuz
Nov 21 at 14:38
What have you tried? SO is not code conversion service.
– Reniuz
Nov 21 at 14:38
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
There are (at least) two ways to do that:
You can use Builder
class to build filter and projection part. The problem is that you can't express Roles.Name
part using strongly typed lambda expressions so you can use FieldDefinition
type as a fallback
var values = new { "PO", "role1", "TO" };
FieldDefinition<Model, string> field = "Roles.Name";
var filter = Builders<Model>.Filter.In(field, values);
var project = Builders<Model>.Projection.Combine(
Builders<Model>.Projection.Include(x => x._id),
Builders<Model>.Projection.Include(x => x.ParentId)
);
var result = Col.Find(filter).Project(project).ToList();
Alternatively you can use LINQ syntax which will be translated to relevant MongoDB command:
var values = new { "PO", "role1", "TO" };
var q = from doc in Col.AsQueryable()
where doc.Roles.Any(x => values.Contains(x.Name))
select new Model6()
{
ParentId = doc.ParentId,
_id = doc._id
};
var result = q.ToList();
1
Thanks a lot It was very informative and much appreciated.I was trying to express Roles.Name in Lambda expression as hard coding the filed name is not a good practice. I understand from your response that it is not possible to express everything in Lambda.That was wonderful to know that we can use Linq in C# to interface with MongoDB. The one issue i could see in Linq is that it not Asynchronous.
– user641247
Nov 22 at 3:24
@user641247 remember about nameof operator in C#, you can build that string having compile time check
– mickl
Nov 22 at 6:05
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
There are (at least) two ways to do that:
You can use Builder
class to build filter and projection part. The problem is that you can't express Roles.Name
part using strongly typed lambda expressions so you can use FieldDefinition
type as a fallback
var values = new { "PO", "role1", "TO" };
FieldDefinition<Model, string> field = "Roles.Name";
var filter = Builders<Model>.Filter.In(field, values);
var project = Builders<Model>.Projection.Combine(
Builders<Model>.Projection.Include(x => x._id),
Builders<Model>.Projection.Include(x => x.ParentId)
);
var result = Col.Find(filter).Project(project).ToList();
Alternatively you can use LINQ syntax which will be translated to relevant MongoDB command:
var values = new { "PO", "role1", "TO" };
var q = from doc in Col.AsQueryable()
where doc.Roles.Any(x => values.Contains(x.Name))
select new Model6()
{
ParentId = doc.ParentId,
_id = doc._id
};
var result = q.ToList();
1
Thanks a lot It was very informative and much appreciated.I was trying to express Roles.Name in Lambda expression as hard coding the filed name is not a good practice. I understand from your response that it is not possible to express everything in Lambda.That was wonderful to know that we can use Linq in C# to interface with MongoDB. The one issue i could see in Linq is that it not Asynchronous.
– user641247
Nov 22 at 3:24
@user641247 remember about nameof operator in C#, you can build that string having compile time check
– mickl
Nov 22 at 6:05
add a comment |
up vote
1
down vote
accepted
There are (at least) two ways to do that:
You can use Builder
class to build filter and projection part. The problem is that you can't express Roles.Name
part using strongly typed lambda expressions so you can use FieldDefinition
type as a fallback
var values = new { "PO", "role1", "TO" };
FieldDefinition<Model, string> field = "Roles.Name";
var filter = Builders<Model>.Filter.In(field, values);
var project = Builders<Model>.Projection.Combine(
Builders<Model>.Projection.Include(x => x._id),
Builders<Model>.Projection.Include(x => x.ParentId)
);
var result = Col.Find(filter).Project(project).ToList();
Alternatively you can use LINQ syntax which will be translated to relevant MongoDB command:
var values = new { "PO", "role1", "TO" };
var q = from doc in Col.AsQueryable()
where doc.Roles.Any(x => values.Contains(x.Name))
select new Model6()
{
ParentId = doc.ParentId,
_id = doc._id
};
var result = q.ToList();
1
Thanks a lot It was very informative and much appreciated.I was trying to express Roles.Name in Lambda expression as hard coding the filed name is not a good practice. I understand from your response that it is not possible to express everything in Lambda.That was wonderful to know that we can use Linq in C# to interface with MongoDB. The one issue i could see in Linq is that it not Asynchronous.
– user641247
Nov 22 at 3:24
@user641247 remember about nameof operator in C#, you can build that string having compile time check
– mickl
Nov 22 at 6:05
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
There are (at least) two ways to do that:
You can use Builder
class to build filter and projection part. The problem is that you can't express Roles.Name
part using strongly typed lambda expressions so you can use FieldDefinition
type as a fallback
var values = new { "PO", "role1", "TO" };
FieldDefinition<Model, string> field = "Roles.Name";
var filter = Builders<Model>.Filter.In(field, values);
var project = Builders<Model>.Projection.Combine(
Builders<Model>.Projection.Include(x => x._id),
Builders<Model>.Projection.Include(x => x.ParentId)
);
var result = Col.Find(filter).Project(project).ToList();
Alternatively you can use LINQ syntax which will be translated to relevant MongoDB command:
var values = new { "PO", "role1", "TO" };
var q = from doc in Col.AsQueryable()
where doc.Roles.Any(x => values.Contains(x.Name))
select new Model6()
{
ParentId = doc.ParentId,
_id = doc._id
};
var result = q.ToList();
There are (at least) two ways to do that:
You can use Builder
class to build filter and projection part. The problem is that you can't express Roles.Name
part using strongly typed lambda expressions so you can use FieldDefinition
type as a fallback
var values = new { "PO", "role1", "TO" };
FieldDefinition<Model, string> field = "Roles.Name";
var filter = Builders<Model>.Filter.In(field, values);
var project = Builders<Model>.Projection.Combine(
Builders<Model>.Projection.Include(x => x._id),
Builders<Model>.Projection.Include(x => x.ParentId)
);
var result = Col.Find(filter).Project(project).ToList();
Alternatively you can use LINQ syntax which will be translated to relevant MongoDB command:
var values = new { "PO", "role1", "TO" };
var q = from doc in Col.AsQueryable()
where doc.Roles.Any(x => values.Contains(x.Name))
select new Model6()
{
ParentId = doc.ParentId,
_id = doc._id
};
var result = q.ToList();
answered Nov 21 at 18:25
mickl
10.4k51435
10.4k51435
1
Thanks a lot It was very informative and much appreciated.I was trying to express Roles.Name in Lambda expression as hard coding the filed name is not a good practice. I understand from your response that it is not possible to express everything in Lambda.That was wonderful to know that we can use Linq in C# to interface with MongoDB. The one issue i could see in Linq is that it not Asynchronous.
– user641247
Nov 22 at 3:24
@user641247 remember about nameof operator in C#, you can build that string having compile time check
– mickl
Nov 22 at 6:05
add a comment |
1
Thanks a lot It was very informative and much appreciated.I was trying to express Roles.Name in Lambda expression as hard coding the filed name is not a good practice. I understand from your response that it is not possible to express everything in Lambda.That was wonderful to know that we can use Linq in C# to interface with MongoDB. The one issue i could see in Linq is that it not Asynchronous.
– user641247
Nov 22 at 3:24
@user641247 remember about nameof operator in C#, you can build that string having compile time check
– mickl
Nov 22 at 6:05
1
1
Thanks a lot It was very informative and much appreciated.I was trying to express Roles.Name in Lambda expression as hard coding the filed name is not a good practice. I understand from your response that it is not possible to express everything in Lambda.That was wonderful to know that we can use Linq in C# to interface with MongoDB. The one issue i could see in Linq is that it not Asynchronous.
– user641247
Nov 22 at 3:24
Thanks a lot It was very informative and much appreciated.I was trying to express Roles.Name in Lambda expression as hard coding the filed name is not a good practice. I understand from your response that it is not possible to express everything in Lambda.That was wonderful to know that we can use Linq in C# to interface with MongoDB. The one issue i could see in Linq is that it not Asynchronous.
– user641247
Nov 22 at 3:24
@user641247 remember about nameof operator in C#, you can build that string having compile time check
– mickl
Nov 22 at 6:05
@user641247 remember about nameof operator in C#, you can build that string having compile time check
– mickl
Nov 22 at 6:05
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414190%2fselecting-fields-in-a-document-after-applying-filter-on-embedded-document%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What have you tried? SO is not code conversion service.
– Reniuz
Nov 21 at 14:38