如何检查mongo数据库中字段的值

How do I check the value of a field in a mongo database

本文关键字:数据库 字段 mongo 何检查 检查      更新时间:2023-09-26

我正在创建一个Node应用程序,该应用程序需要检查集合的字段并返回数据类型。例如,如果字段是"First Name",则数据类型将是"String"。我如何开始创建这样的后端应用程序呢?

如果您使用mongoose,则每个字段或嵌套字段通过path寻址。

var myschema = new Schema({
  ...
  name: {
       first:{type: String, required: true,},
       last :{type: String, required: true,},
  ...
});

这里的名字。第一个和名称。.

现在了解名称的类型。最后还有一个Schema API,叫做path()。所以

var pathmeta =  myschema.path(name.last);
console.log(" datatype  = "+pathmeta.instance);
console.log(" whole pathmeta  structure is  "+JSON.stringify(pathmetas));

应该打印这个…

datatype = String

整个pathmeta结构是
{"enumValues":[],"正则表达式":空,"路径":"文本"、"实例":"弦"、"验证":[],"setter":[],"getter":[],"选项":{},"_index":零}