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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:44:18+00:00 2026-06-04T16:44:18+00:00

I’m trying to make a query that must to retrieve some data from my

  • 0

I’m trying to make a query that must to retrieve some data from my mongodb database, follow my code:

 72     mongo conn[1];
 73     mongo_cursor cursor[1];
 74 
 75     //Parameters (connection, IP, port);
 76     int status = mongo_connect(conn, "127.0.0.1", 27017);
 77     if(status != MONGO_OK){
 78         switch(conn->err){
 79             case MONGO_CONN_NO_SOCKET: printf("Socket not found\n"); return 1;
 80             case MONGO_CONN_FAIL: printf("Connection Failed\n"); return 1;
 81             case MONGO_CONN_NOT_MASTER: printf("Not master\n"); return 1;
 82         }
 83     }
 84 
 85     //add_new_service(conn, 55, 75,"java","7");
 86     bson query[1];
 87     bson_init(query);
 88     bson_append_start_object(query, "$query");
 89         bson_append_int(query,"cpu",75);
 90     bson_append_finish_object(query);
 91     bson_finish(query);
 92 
 93     bson fields[1];
 94     bson_init(fields);
 95     bson_append_null(fields,"machine");
 96     bson_finish(fields);
 97 
 98     mongo_cursor_init(cursor, conn, "db.services");
 99 
100     cursor = mongo_find(conn,"db.services", query, fields, 9999, 0, 0);
101 
102     while(mongo_cursor_next(ptr_cursor) == MONGO_OK){
103         bson_print(&ptr_cursor->current);
104     }
105 
106     mongo_cursor_destroy(cursor);
107     mongo_destroy(conn);
108     return 0;

I don’t know how can i use the method “mongo_find” (line 100), it returns a pointer to a mongo_cursor, but i can’t manipulate this pointer, some ideas to help me achieve this?

  • 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-06-04T16:44:19+00:00Added an answer on June 4, 2026 at 4:44 pm

    Below is working code that does what I think that you want.
    Please note the following:

    1. $query as an operator or command does not exist, just use the inner part of your BSON
    2. the query uses “machine.cpu” since “cpu” is in a sub-document of “machine”
    3. for fields, I’ve changed to bson_append_bool instead of bson_append_null so that you have results for the bson_find below
    4. for your bson_find, you have to navigate into machine since it is a sub-document that contains “cpu”

    Hope that this moves you ahead.

    #include "mongo.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    main(int argc, char** argv)
    {
        mongo conn[1];
        mongo_cursor* cursor;
    
        //Parameters (connection, IP, port);
        int status = mongo_connect(conn, "127.0.0.1", 27017);
        if(status != MONGO_OK){
            switch(conn->err){
                case MONGO_CONN_NO_SOCKET: printf("Socket not found\n"); return 1;
                case MONGO_CONN_FAIL: printf("Connection Failed\n"); return 1;
                case MONGO_CONN_NOT_MASTER: printf("Not master\n"); return 1;
            }
        }
    
        //add_new_service(conn, 55, 75,"java","7");
        bson query[1];
        bson_init(query);
        bson_append_int(query,"machine.cpu",75);
        bson_finish(query);
    
        bson fields[1];
        bson_init(fields);
        bson_append_bool(fields,"machine",1);
        bson_finish(fields);
    
        //mongo_cursor_init(cursor, conn, "db.services");
    
        cursor = mongo_find(conn,"db.services", query, fields, 9999, 0, 0);
    
        while(mongo_cursor_next(cursor) == MONGO_OK){
            bson_print(mongo_cursor_bson( cursor ));
            bson_iterator it;
            if (bson_find(&it, &cursor->current, "machine")){
                bson sub;
                bson_iterator_subobject( &it, &sub );
                if (bson_find(&it, &sub, "cpu")){
                    printf("bson find cpu: %d\n", bson_iterator_int(&it));
                }
            }
        }
    
        mongo_cursor_destroy(cursor);
        mongo_destroy(conn);
        return 0;
    }
    

    output

        _id : 7      4fc51ba2db00e4ba36334713
        machine : 3
            cpu : 16     75
            ram : 16     55
    
    bson find cpu: 75
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.