node.jsgoogle-cloud-platformgoogle-cloud-datastore

Can you provide an example of how to use "IN" queries in Node.js with Google Cloud Datastore?


I want to explain how to use the "IN" search in Node.js.
I understand that the "IN" operator may not be supported in Node.js.
https://cloud.google.com/datastore/docs/concepts/queries#in

Someone help me.

Here is the code I tried.

const idList = [1, 2, 3];
const query = this.dataStoreClient.createQuery('namespace', 'kind')
  .filter('ID', 'in', idList);

An error message appeared here, for sure.

Error: 3 INVALID_ARGUMENT: a property filter must specify an operator

Solution

  • As per this reference- https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/query

    But you check the link- https://cloud.google.com/datastore/docs/concepts/queries#datastore-datastore-property-filter-nodejs

    IN comparison operator we can use. enter image description here

    Below is an example of how to use "IN" queries in Node.js with Google Cloud Datastore:

        // Import the Google Cloud Datastore client
    
        const Datastore = require('@google-cloud/datastore');
       // Create an instance of a Datastore client
       const datastore = new Datastore();
    
       // Create an array of the values you want to query
       const ids = [1, 2, 3, 4, 5];
    
       // Create a query
       const query = datastore
          .createQuery('YourEntityName')
          .filter('id', 'IN', ids);
    
        // Run the query
        datastore.runQuery(query).then(results => {
      const entities = results[0];
    
          console.log('Entities:');
      entities.forEach(entity => {
        console.log(entity);
      });
    });