Search code examples
node.jsmongodbgraphqlfeathersjs

Mongodb graphQL and Feather js data fetch by _id return null for some case


I'm trying to fetch records on the basis of _id in mongoDB, In mongoDB console it working fine, but when I try to implement the same code in GraphQL and Feather js, it returns null, record not found, but records is in DB. If i change the value to 507F191M810c19729De860eA is return record. It works for a few values. It's a very Weird issue.

Locations.find(
      { 
        query: 
        { 
          _id: "2313891acb3420defAFadefc" 
        }
      }
    ).then(function(result){
      console.log(result)
    }

Solution

  • Please try ti find mongoId by convert into objcectId. You are passing id as a simple string. May be you are not getting the data.

    var ObjectID = require('mongodb').ObjectID;
    

    Now you can convert string into mongo's id.

    Locations.find(
      { 
        Query: 
        { 
          _id: ObjectID("2313891acb3420defAFadefc") 
        }
      }
    ).then(function(result){
      console.log(result)
    }