我想使用一个索引,然后看到了这个:

class Products_ByCategoryName_JS extends AbstractJavaScriptIndexCreationTask {
    constructor () {
        super();

        const { load } = this.mapUtils();

        this.map("Products", product => {
            return {
                // Call method 'load' to load the related Category document
                // The document ID to load is specified by 'product.Category'
                // The Name field from the related Category document will be indexed                
                categoryName: load(product.Category, "Categories").Name

                // Since NoTracking was Not specified,
                // then any change to either Products or Categories will trigger reindexing
            };
        });
    }
}

这就是我执行查询的方式

const matchingProducts = await session
    .query({indexName: "Products/ByCategoryName"})
    .whereEquals("CategoryName", "Beverages")
    .all();

它来自文档,好的,但是我如何执行Products_ByCategoryName_JS上面的索引类,

那么它在哪里执行,或者我可以在我的代码中的哪里包含它呢?

EURO:收到此错误:

InvalidArgumentException: Map is required to generate an index, you cannot create an index without a valid Map property (in index Users/ByName)

索引:

// @ts-nocheck
import { AbstractJavaScriptIndexCreationTask } from "ravendb";

export class Users_ByName extends AbstractJavaScriptIndexCreationTask<{name: string}[]> {
  constructor () {
      super();

      this.map = `
        from users in docs.UsersData
        select new {
            Name = users.name
        }`;
  }
}

帖子主题:回复:

app.get('/', async (req, res) => {
  try {
    await new Users_ByName().execute(store);
    let session = store.openSession();
    const queryOnIndex = session.query({indexName: 'UsersData/ByName'}).whereEquals('name', 'Johnse');

    const d = await queryOnIndex.all();
    res.json('s');
  } catch(e) {
    console.log(e);
    res.send(e);
  }
});

我做错了什么? 我的集合名称是UsersData,我的字段名称是我想要索引的名称

推荐答案

要执行索引/将索引部署到服务器,请调用:

await new Products_ByCategoryName_JS().execute(documentStore);

See demo "static indexes overview" in the Node.js client:
https://demo.ravendb.net/demos/nodejs/static-indexes/static-indexes-overview https://demo.ravendb.net/demos/nodejs/static-indexes/static-indexes-overview#step-3


在您提供的最新示例中--索引类名是:Users_ByName,但在查询中,您使用的是索引名:UsersData/ByName.

在查询中,您应该使用‘class’中的索引名,并且只将_替换为/.
因此,在您的查询中应该使用:session.query({indexName: 'Users/ByName'})


More info about creating and deploying indexes is available in:
https://ravendb.net/docs/article-page/latest/nodejs/indexes/creating-and-deploying
https://ravendb.net/docs/article-page/latest/nodejs/indexes/creating-and-deploying#naming-convention

Node.js相关问答推荐

使用HTTPS从NodeJS 17.9.1升级到18.0.0后,SignalR连接失败

正在try 使用Azure Function App上载文件时未上载文件(&Q;)

(0,core_1.default)不是使用@middy/core的lambda处理程序上的函数

如何在Angular jest测试中调用Nodejs的垃圾收集? node v20的测试速度慢且内存泄漏

用于SLACK命令返回json而不是文本的AWS lambda函数

后端如何保护数据不被用户提取其他用户的数据?

创建查询以展开数组并筛选出具有特定值的元素之后的元素

mongodb首先自连接,然后根据某些条件与另一个表连接

Nestjs swc 错误:找不到模块项目路径/src/app.module

Node.js 连接在查询完成之前终止

如果我在父文件夹中运行,子进程生成不起作用

处理嵌套元素时,使用xml2js库解析XML时发生错误

如何在 node /快速服务器上配置 mongoDB

我应该如何解决这个 Angular node 包模块依赖冲突?

为什么 $or 在带有正则表达式的mongoose 中不能正常工作

将 AllowDiskUse true 添加到 node.js 中的 MongoDB 聚合?

带权限的机密 Rest-Api - 总是 403 - 我做错了什么?

将 NodeJS 用于大型项目

node.js 找不到模块mongodb

为什么 Node.js 没有原生 DOM?