【问题标题】:How to fetch NumberLong record in mongoose如何在猫鼬中获取 NumberLong 记录
【发布时间】:2017-07-15 10:29:54
【问题描述】:

我的文档中有一个字段为

 "p_id" : NumberLong(2328703838)

我尝试如下获取它

spec.find({p_id: id})

我的 id 是 2328703838...但即使存在数据我也无法获取记录。谁能提供帮助。谢谢。

【问题讨论】:

    标签: mongodb mongoose mean-stack


    【解决方案1】:

    您必须使用 mongoose-long 才能在 mongoose 中获得 Number Long 支持。

    将您的数据类型定义为 SchemaTypes.Long 并使用 Long.fromString("2328703838") 创建一个 Long 对象:

    var mongoose = require('mongoose')
    require('mongoose-long')(mongoose);
    
    var SchemaTypes = mongoose.Schema.Types;
    var Long = mongoose.Types.Long;
    
    var SpecSchema = new mongoose.Schema({
        p_id: SchemaTypes.Long
    });
    
    var Spec = mongoose.model('Spec', SpecSchema);
    
    var db = mongoose.connect('localhost', 'testDB');
    
    var id = Long.fromString("2328703838");
    
    Spec.find({ p_id: id }, function(err, res) {
        if (err) {
            console.log(err);
            return;
        }
        console.log(res);
    });
    

    【讨论】:

    • 我真的不明白他们为什么要为每个数据类型创建一个单独的 npm 包 NumberIntNumberLong 是 MongoDB 内置支持的类型,为什么不在 mongoose 中支持它们呢?
    • 如何在 es6 中使用 mongoose-long 插件?
    猜你喜欢
    • 2017-06-05
    • 1970-01-01
    • 2017-05-20
    • 2017-08-31
    • 2013-09-15
    • 2021-06-20
    • 2017-01-09
    • 2020-03-31
    • 2017-11-24
    相关资源
    最近更新 更多