如何使用节点在mongo中插入长值?

C.K*_*hik 4 mongodb node.js

我需要在 mongo 中插入一个属性的 Long 值。

  var sequences = this.db.collection('sequences');
  sequences.insert( {
      _id: "TEST_SEQ",
      value: 1
  }, done);
Run Code Online (Sandbox Code Playgroud)

但这是以整数形式插入值,如何将其变为Long?

Yak*_*ein 7

使用 mongodbLong类。像这样的东西。

const Long = require('mongodb').Long;

var sequences = this.db.collection('sequences');
sequences.insert( {
    _id: "TEST_SEQ",
    value: Long.fromInt(1)
}, done);
Run Code Online (Sandbox Code Playgroud)

看看这里了解更多详情

Mongo-db 有默认类型,它给出值,这就是它作为整数插入的原因。


wdb*_*ley 5

使用 NumberLong 构造函数:

sequences.insert({ "value" : NumberLong(322) }, cb)
Run Code Online (Sandbox Code Playgroud)

  • 需要哪个 require 语句? (3认同)