【问题标题】:How do I check the value of a field in a mongo database如何检查 mongo 数据库中字段的值
【发布时间】:2016-08-26 20:17:50
【问题描述】:

我正在创建一个需要检查集合字段并返回数据类型的 Node 应用程序。例如,如果字段是“First Name”,则数据类型将为“String”。我将如何开始创建执行此操作的后端应用程序?

【问题讨论】:

  • 您的问题需要更加精确。到目前为止,我有点相信您应该只阅读 mongo 文档,例如 mongoose 库文档。

标签: javascript node.js mongodb nosql


【解决方案1】:

如果您使用的是 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}

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 2013-08-22
    • 2023-03-15
    • 2018-03-31
    • 2018-09-16
    • 2017-11-22
    • 2016-05-31
    • 2013-05-08
    • 2012-12-30
    相关资源
    最近更新 更多