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 4621262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:40:32+00:00 2026-05-22T02:40:32+00:00

I am reading a single document into a BsonDocument object. Having read the document

  • 0

I am reading a single document into a BsonDocument object. Having read the document from MongoDB I’d like to query the document in-memory.
My doc looks like this:

{
  "_id": {
    "$binary": "DYibd4bSz0SFXTTmY46gOQ==",
    "$type": "03"
  },
  "title": "XYZ 2011",
  "pages": [
    {
      "pagetype": "contactcapture",
      "pagetitle": "Contact",
      "questions": [
        {
          "qtype": "text",
          "text": "Firstname",
          "name": "firstname"
        },
        {
          "qtype": "text",
          "text": "Surname",
          "name": "surname"
        },
        {
          "qtype": "text",
          "text": "Company",
          "name": "companyname"
        }
      ]
    },
    {
      "pagetype": "question",
      "pagetitle": "Question 1",
      "questions": [
        {
          "qtype": "radio",
          "text": "What drink?",
          "name": "drink",
          "answers": [
            {
              "text": "Tea"
            },
            {
              "text": "Coffee"
            },
            {
              "text": "Hot chocolate"
            },
            {
              "text": "Water"
            }
          ]
        }
      ]
    },
    {
      "pagetype": "question",
      "pagetitle": "Question 2",
      "questions": [
        {
          "qtype": "check",
          "text": "Accompaniments?",
          "name": "accompaniments",
          "answers": [
            {
              "text": "Nuts"
            },
            {
              "text": "Crisps"
            },
            {
              "text": "Biscuits"
            }
          ]
        }
      ]
    },
    {
      "pagetype": "question",
      "pagetitle": "Question 3",
      "questions": [
        {
          "qtype": "radio",
          "text": "When would you like that?",
          "name": "when",
          "answers": [
            {
              "text": "Immediately"
            },
            {
              "text": "10 minutes"
            },
            {
              "text": "Half-an-hour"
            }
          ]
        },
        {
          "qtype": "text",
          "text": "Anything else with that?",
          "name": "anythingelse"
        }
      ]
    }
  ]
}

I want to get all pages that have a pagetype=”question”. I’m currently doing it as follows:

BsonDocument profileDocument= profilesCollection.FindOneByIdAs<MongoDB.Bson.BsonDocument>(binaryId);
BsonElement pagesElement = profileDocument.GetElement("pages");
BsonArray pages=profileDocument.GetElement("pages").Value.AsBsonArray;
foreach (BsonValue pageV in pages.Values)
{
    BsonDocument page = pageV.AsBsonDocument;
    if (page["pagetype"].AsString == "question")
    {
        sb.Append("<br />Question Page:" + page.ToJson());
    }
}

The code seems a little verbose and complex – I just wondered if there is a better way of doing this? Thanks

  • 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-22T02:40:33+00:00Added an answer on May 22, 2026 at 2:40 am

    Assuming that the data type of profilesCollection is MongoCollection<BsonDocument>, you could shorten the code so something like this:

            var profileDocument = profilesCollection.FindOneById(binaryId);
            foreach (BsonDocument page in profileDocument["pages"].AsBsonArray) {
                if (page["pagetype"].AsString == "question") {
                    sb.Append("<br />Question Page:" + page.ToJson());
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way of reading one single character from the user input? For
From my reading I am not sure if AES is a single, standardized algorithm
Reading and writing of a single variable is atomic (language guarantee!), unless the variable
I'm reading all the files in a single directory and I want to filter
Reading an article called Increase LINQ Query Performance in July's MSDN magazine, the author
I've been reading about the module pattern, but everything I read assumes that the
Reading this question I found this as (note the quotation marks) code to solve
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading through the Flickr API documentation it keeps stating I require an API key
Reading MSDN (and other sources) about custom report items (CRI) for reporting services 2005.

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.