简体   繁体   English

使用Node.js的Google Cloud Datastore查询

[英]Google Cloud Datastore Queries using Node.js

There is a method to get all the data using Node.js and Google Cloud Datastore. 有一种使用Node.js和Google Cloud Datastore获取所有数据的方法。

var query = ContactModel.query();//contact model is a schema which instantiates gstore schema

query.run().then((result) => {
    const response = result[0];
     var entities       = response.entities;
    callback('',entities);
});

Is there a way to run custom query or simply say filters using Node.js and Google Cloud Datastore. 有没有一种方法可以运行自定义查询,或者只是使用Node.js和Google Cloud Datastore说过滤器。 I could find only one query example using Node.js and Google Cloud Datastore. 我只能使用Node.js和Google Cloud Datastore找到一个查询示例。

var query = ContactModel.query()

query.filter(key, value)

query.run().then((result) => {
  const response = result[0],
        entities = response.entities

  callback('',entities)
})

The filter function will filter with respect to the key and value, filter function takes an optional third argument, the third one being the condition like equal to, less than, etc.. but if you provide only two arguments, it will check for equality. filter函数将根据键和值进行过滤,filter函数接受一个可选的第三个参数,第三个参数是等于,小于等条件,但是如果仅提供两个参数,它将检查是否相等。 There are other functions like limit, order, groupBy etc. Find the documentation here . 还有其他功能,例如限价,定单,分组依据等。在此处查找文档。

I suggest you use gstore-node , it has a very simple and efficient API, and the documentation has many examples of how to make advanced querys 我建议您使用gstore-node ,它具有非常简单和有效的API,并且文档中包含许多有关如何进行高级查询的示例。

For example in the documentation it is detailed how to list all the elements 例如,在文档中 ,详细说明了如何列出所有元素

// blog-post.model.js

// Create Schema
const blogPostSchema = new gstore.Schema({
    title : { type: 'string' },
    isDraft: { type: 'boolean' }
});

// List query settings
const listQuerySettings = {
    limit : 10,
    order : { property: 'title', descending: true }, // descending defaults to false and is optional
    select : 'title',
    ancestors : ['Parent', 123],  // will add an "hasAncestor" filter
    filters : ['isDraft', false] // operator defaults to "=",
};

// Add settings to schema
blogPostSchema.queries('list', listQuerySettings);

// Create Model
const BlogPost = gstore.model('BlogPost', blogPostSchema);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM