Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 598539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:23:57+00:00 2026-05-13T16:23:57+00:00

It seems I don’t understand how I can get a value from a collection

  • 0

It seems I don’t understand how I can get a value from a collection inside a document. I am using mongoDB in C#.

Here is my code:

var jimi = new Document();

jimi["Firstname"] = "Jimi";
jimi["Lastname"] = "James";
jimi["Pets"] = new[]
{
    new Document().Append("Type", "Cat").Append("Name", "Fluffy"),
    new Document().Append("Type", "Dog").Append("Name", "Barky"),
    new Document().Append("Type", "Gorilla").Append("Name", "Bananas"),
};

test.Insert(jimi);

var query = new Document().Append("Pets.Type","Cat");

So my query will look for the pet cat. But I am not sure how I can get the name of my cat. I tried a few things but I mostly get the whole document back.

Thanks in advance,

Pickels

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:23:58+00:00Added an answer on May 13, 2026 at 4:23 pm

    This isn’t as elegant as I’d like as I’m still learning about MongoDB myself but it does show you one way to get the property you wanted.

    [TestFixture]
    public class When_working_with_nested_documents
    {
        [Test]
        public void Should_be_able_to_fetch_properties_of_nested_objects()
        {
            var mongo = new Mongo();
            mongo.Connect();
            var db = mongo.getDB("tests");
            var people = db.GetCollection("people");
    
            var jimi = new Document();
    
            jimi["Firstname"] = "Jimi";
            jimi["Lastname"] = "James";
            jimi["Pets"] = new[]
            {
                new Document().Append("Type", "Cat").Append("Name", "Fluffy"),
                new Document().Append("Type", "Dog").Append("Name", "Barky"),
                new Document().Append("Type", "Gorilla").Append("Name", "Bananas"),
            };
    
            people.Insert(jimi);
    
            var query = new Document();
            query["Pets.Type"] = "Cat";
            var personResult = people.FindOne(query);
            Assert.IsNotNull(personResult);
            var petsResult = (Document[])personResult["Pets"];
            var pet = petsResult.FindOne("Type", "Cat");
            Assert.IsNotNull(pet);
            Assert.AreEqual("Fluffy", pet["Name"]);
        }
    }
    
    public static class DocumentExtensions
    {
        public static Document FindOne(this Document[] documents, string key, string value)
        {
            foreach(var document in documents)
            {
                var v = document[key];
                if (v != null && v.Equals(value))
                {
                    return document;
                }
            }
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't understand why the code isn't working, and nothing I found seems to
I don't quite understand whats happening here a=[1,2,3,4] b=[5,6,7,8] a[len(a):] = b seems to
Using object literals for the first time and it seems I don't quite understand
It seems I still don't get generics ... I want a general function to
It's a petty basic question which seems I don't fully understand, What exactly is
It seems I don't understand javascript callbacks quite as well as I thought. In
I have been googlgling around and it seems I don't get any answer :/
I'm sorry if this is a newbie question, but it seems I don't get
It seems that I don't understand the meaning of the in keyword in JavaScript.
I'm having some trouble with strings in Golang. It seems that they don't get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.