logo
down
shadow

Get mongodb subDocument


Get mongodb subDocument

By : TTK
Date : November 21 2020, 11:01 PM
help you fix your problem Firstly it appear you are querying a Username field on your document which you do not appear to have based on the data structure you have provided above:
code :
.Find(Query.EQ("userName", "Sohail"))
var collection = mongodb.GetCollection<QuestionsList>("Papers");

var projection = Builders<QuestionsList>.Projection.Include("Questions");
var choice = collection
                 .Find(Query.EQ("userName", "Sohail"))
                 .Project(projection)
                 .ToList();

return choice;
public class QuestionsList
{
    [BsonRepresentation(BsonType.ObjectId)]
    [BsonId]
    public string Id { get; set; }

    //Missing from your data structure but assume it is there
    public string userName { get; set; }

    public string Status { get; set; }

    public List<Question> Questions { get; set; }
}

public class Question
{
    public string QuestionType { get; set; }
    public string Question { get; set; }
    public string Correct { get; set; }
    public List<string> Options { get; set; }
}
var choice = collection
                 .Find(x => x.userName == "Sohail"))
                 .Project(x => x.Questions)
                 .ToList();


Share : facebook icon twitter icon
MongoDB remove a subdocument document from a subdocument

MongoDB remove a subdocument document from a subdocument


By : user3262602
Date : March 29 2020, 07:55 AM
it helps some times I use 10gen C# driver for MongoDB and I would like to remove a subdocument from a subdocument. I don't know how to do it. , you should to use $pull operation for delete item from array.
code :
        var query = Query.And(Query.EQ("_id", applicationId),
                         Query.EQ("Settings.Key",  "ImportDirectory"));
        var update = Update.Pull("Settings.$.Overrides", new BsonDocument(){
            { "Name", "PathDirectory" }
        });
        database.Applications().Update(query, update);
mongodb: remove subdocument with where clause that includes document & subdocument

mongodb: remove subdocument with where clause that includes document & subdocument


By : Sandeep Singh
Date : March 29 2020, 07:55 AM
hop of those help? Here is my collection (workers): , The syntax of your $pull object is off. Try this instead:
code :
db.workers.update({"type":"Manager","employees.id":101},
    {$pull : {"employees" : {"id" : 103}}},false,true)
db.workers.find({
    type: "Manager", 
    $and: [{'employees.id': 101}, {'employees.id': 103}]
})
Querying subdocument inside a subdocument of a collection in Mongodb

Querying subdocument inside a subdocument of a collection in Mongodb


By : user3851758
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You need to use $elemMatch to get required output as following:
code :
db.collection.find({
    "categories": {
    $elemMatch: {
        subcategories: {
            $elemMatch: {
                CatergoryName: "C1S1category1"
            }
        }
    }
    }
},{"categories.$":1,"companyName":1,"url":1}).pretty()
mongodb select parent subdocument based on child subdocument's array

mongodb select parent subdocument based on child subdocument's array


By : katlew
Date : March 29 2020, 07:55 AM
I hope this helps you . You can try below aggregation query
$match to find documents with volunteers email
code :
aggregate({
    $match: {
        "shifts.volunteers.email": "mary@mack.com"
    }
}, {
    $project: {
        where: 1,
        shifts: {
            $let: {
                vars: {
                    doc: {
                        $arrayElemAt: ["$shifts", 0]
                    }
                },
                in: {
                    date: "$$doc.dateNeeded",
                    time: "$$doc.timeslot",
                    details: "$$doc.details"
                }
            }
        }
    }
})
aggregate({
    $match: {
        "shifts.volunteers.email": "mary@mack.com"
    }
}, {
    $project: {
        where: 1,
        shift: {
            $arrayElemAt: ["$shifts", 0]
        }
    }
}, {
    $project: {
        where: 1,
        date: "$shift.dateNeeded",
        time: "$shift.timeslot",
        details: "$shift.details"
    }
})
MongoDB upsert in subdocument in array and return the updated subdocument

MongoDB upsert in subdocument in array and return the updated subdocument


By : Pedro
Date : March 29 2020, 07:55 AM
To fix this issue Run the findAndModify command with the parameter new set to true and thus returns the modified document rather than the original.
The update should use the $ positional operator which identifies an element in an array to update without explicitly specifying the position of the element in the array. For this to work the array field must appear as part of the query document.
Related Posts Related Posts :
  • ASP.NET MVC database connection using a form
  • IIS7.5 max-age issue(asp.net mvc output cache)
  • Why i can not send parameter to controller with ajax?
  • Kendo UI Grid data bind to two table DB
  • How to host ASP.NET MVC app on IIS10 win10
  • Bind viewmodel to partial view
  • Error while adding a wcf service refrence in ASP.Net mvc
  • MVC how to submit value from dropdown list
  • How to switch between two postgres connection strings using MVC5
  • Exclude directory from MSBuild Typescript compilation
  • templateUrl in Angular2 Component not loading URL
  • combine two routes to one action
  • Multiple Route registration in Asp.net MVC Area
  • How to Keep Track User Last Activity Time Using Microsoft.AspNet Identity
  • How can I pass parameters using request Header with aspnet boilerplate?
  • C# ASP.NET Identity 2 (.NET Framework MVC) - Using permission AND roles
  • How do I get IIS running on Windows 1709 (build 16299.15)
  • Can I use ILoggerFactory in ASP.NET MVC 5 (not mvc core) project?
  • How to turn the EditorFor into DATETIME picker?
  • check the checkbox based on model in mvc?
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com