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"
}









share|improve this question
























  • What have you tried? SO is not code conversion service.
    – Reniuz
    Nov 21 at 14:38















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"
}









share|improve this question
























  • What have you tried? SO is not code conversion service.
    – Reniuz
    Nov 21 at 14:38













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"
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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





share|improve this answer

















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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





share|improve this answer

















  • 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















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





share|improve this answer

















  • 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













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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














  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)