【发布时间】:2016-08-26 20:17:50
【问题描述】:
我正在创建一个需要检查集合字段并返回数据类型的 Node 应用程序。例如,如果字段是“First Name”,则数据类型将为“String”。我将如何开始创建执行此操作的后端应用程序?
【问题讨论】:
-
您的问题需要更加精确。到目前为止,我有点相信您应该只阅读 mongo 文档,例如 mongoose 库文档。
标签: javascript node.js mongodb nosql
我正在创建一个需要检查集合字段并返回数据类型的 Node 应用程序。例如,如果字段是“First Name”,则数据类型将为“String”。我将如何开始创建执行此操作的后端应用程序?
【问题讨论】:
标签: javascript node.js mongodb nosql
如果您使用的是 mongoose ,则每个字段或嵌套字段都由路径寻址。
var myschema = new Schema({
...
name: {
first:{type: String, required: true,},
last :{type: String, required: true,},
...
});
这里 name.first 和 name.last 是路径。
现在要知道 name 的类型。最后有一个 Schema API,称为 path().So。
var pathmeta = myschema.path(name.last);
console.log(" datatype = "+pathmeta.instance);
console.log(" whole pathmeta structure is "+JSON.stringify(pathmetas));
应该打印这个..
数据类型 = 字符串
整个路径元结构是
{"enumValues":[],"regExp":null,"path":"text","instance":"String","validators":[],"setters":[],"getters":[] ,"options":{},"_index":null}
【讨论】: