MongoDB C driver how to find object field value in array of objects

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • Priority: Minor - P4
    • None
    • Affects Version/s: 1.6.0
    • Component/s: libmongoc
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Hi. everyone.

      Query for a field in an object in array with Mongo?
      i have this json and i want check if the value of sessions.id == 1234556:

      {
          "_id": ObjectId("5768e43"),
          "APPID": {
              "Number": 0,
          },
          sessions: [
              {
                  id:1234556,
                  "setOID": {
                      "Number": 0 
      
                  },
                  "custID": {
                      "Number": 0 
                  },
              },
              {
                  id:133333,
                  "setOID": {
                      "Number": 2 
                  },
                  "custID": {
                      "Number": 2 
                  },
              },
           ]
      }
      
      query = BCON_NEW ("$query", "{", "session.id", 1234556,"}");
      cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
      
       the result is like this:
      
      {
          "_id": ObjectId("5768e43"),
          "APPID": {
              "Number": 0,
          },
          sessions: [
              {
                  id:1234556,
                  "setOID": {
                      "Number": 0 
      
                  },
                  "custID": {
                      "Number": 0 
                  },
              },
              {
                  id:133333,
                  "setOID": {
                      "Number": 2 
                  },
                  "custID": {
                      "Number": 2 
                  },
              },
           ]
      }
      
      but I want this result.
      
      {
          "_id": ObjectId("5768e43"),
          "APPID": {
              "Number": 0,
          },
          sessions: [
              {
                  id:1234556,
                  "setOID": {
                      "Number": 0 
      
                  },
                  "custID": {
                      "Number": 0 
                  },
              }
           ]
      }
      

            Assignee:
            Unassigned
            Reporter:
            eh.y
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: