本文介绍了猫鼬ODM:未定义NumberInt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB集合测试"包含一条记录:

MongoDB collection "test" contains a record:

{ "_id" : ObjectId("56f53cded3095c203024a884"), status : 0 }

我通过Node.js(猫鼬驱动程序)连接到mongo.定义此集合的模式:

I was connected to mongo by Node.js (Mongoose driver). Define schema for this collection:

"Test" : {
    status : { type : Number, default : 0 }
}

字段状态"是一个二进制标志,必须仅存储0或1个数字值.我想用更新查询更改此标志:

Field "status" is a binary flag, must be store only 0 or 1 number value. I wanna change this flag with update query:

test.update( { 'status' : 0 }, { $bit : { 'status' : { xor : NumberInt(1) } } }, function( err, result ) {
    if( err ) {
        console.log( 'err' );
    }
});

找不到猫鼬NumberInt()函数:

ReferenceError: NumberInt is not defined

当我将简单数字"1"放在没有功能NumberInt()的情况下,我没有任何更改或错误.

When i put simple number "1" without function NumberInt(), i not have any changes or errors.

在MongoDB shell中,多数民众赞成在查询工作完美.我如何在Mongoose驱动程序中使用NumberInt()?它有别的名字吗?

In MongoDB shell thats query worked perfectly. How i can work with NumberInt() in Mongoose driver? Is it have another name?

推荐答案

NumberInt的讨论. "nofollow>此功能请求在GitHub上的Mongoose存储库中.然后通过mongoose-int32 NPM软件包(此处)将其作为插件添加.通过安装然后要求使用类似以下内容的模块,您可以实现与MongoDB的NumberInt相同的行为.

There was some discussion about supporting NumberInt on this feature request in the Mongoose repository on GitHub. It was then added as a plugin through the mongoose-int32 NPM package available here. You can achieve the same behaviour as MongoDB's NumberInt by installing and then requiring the module using something like the following.

var NumberInt = require('mongoose-int32');

这篇关于猫鼬ODM:未定义NumberInt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 12:52