NodeJS和Mongoskin,不能做简单的更新。传入的参数必须是12个字节或24个十六进制字符串

时间:2022-08-18 15:47:05

Using mongoskin.

I'm trying to do a simple update and I keep on getting the error:

我正在尝试进行简单的更新,并继续收到错误消息:

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format

错误:传入的参数必须是12个字节的单个字符串或十六进制格式的24个十六进制字符的字符串

Different code that I have tried:

我试过的不同代码:

var mongo = require('mongoskin'),
    store = mongo.db(MONGO_DB_ADDESS + ':' + MONGO_DB_PORT + '/' + MONGO_DB_NAME + '?auto_reconnect=false');

session._id = 4eb5444d39e153e60b000001;

store.collection('sessions').updateById({_id : session._id}, {$set: status_obj}, {upsert : false, multi : false, safe : false}, function() { ... });

store.collection('sessions').updateById(session._id, {$set: status_obj} );      

Even tried:

store.collection('sessions').update( {'_id': session._id}, {$set: {"status":'unavailable'}} );    

Any help appreciated!

任何帮助赞赏!

Thanks Fyi, I can do the update via mongo using the cli just fine:

谢谢Fyi,我可以通过mongo使用cli进行更新:

db.sessions.update( {'_id': ObjectId('4eb5444d39e153e60b000001')}, {$set: {"status":'unavailable'}} );

4 个解决方案

#1


17  

store.collection('sessions').updateById(session._id.toString(), {$set: status_obj} ); 

Adding .toString() finally resolved this for me.

添加.toString()最终解决了这个问题。

#2


3  

Did have a similar error with Mongoose, it turned out my id was wrong, but maybe this validation function can help your:

与Mongoose有类似的错误,结果我的id错了,但也许这个验证功能可以帮助你:

function validate_id(id) {
  return !(id !== null && (id.length != 12 && id.length != 24));
}

#3


1  

put this in the root of your javascript

把它放在你的javascript的根目录中

ObjectID = require('mongoskin').ObjectID;

ObjectID = require('mongoskin')。ObjectID;

collection.updateById(new ObjectID(song._id), <json>, <callback>);

collection.updateById(new ObjectID(song._id), , );

puts the _id you get in node into the format mongo needs

将你在节点中获得的_id放入mongo需要的格式中

#4


0  

You can also try using trim() to remove whitespace before or after the string, which will cause the same original error:

您还可以尝试使用trim()删除字符串之前或之后的空格,这将导致相同的原始错误:

store.collection('sessions').updateById(session._id.toString().trim(), {$set: status_obj} ); 

#1


17  

store.collection('sessions').updateById(session._id.toString(), {$set: status_obj} ); 

Adding .toString() finally resolved this for me.

添加.toString()最终解决了这个问题。

#2


3  

Did have a similar error with Mongoose, it turned out my id was wrong, but maybe this validation function can help your:

与Mongoose有类似的错误,结果我的id错了,但也许这个验证功能可以帮助你:

function validate_id(id) {
  return !(id !== null && (id.length != 12 && id.length != 24));
}

#3


1  

put this in the root of your javascript

把它放在你的javascript的根目录中

ObjectID = require('mongoskin').ObjectID;

ObjectID = require('mongoskin')。ObjectID;

collection.updateById(new ObjectID(song._id), <json>, <callback>);

collection.updateById(new ObjectID(song._id), , );

puts the _id you get in node into the format mongo needs

将你在节点中获得的_id放入mongo需要的格式中

#4


0  

You can also try using trim() to remove whitespace before or after the string, which will cause the same original error:

您还可以尝试使用trim()删除字符串之前或之后的空格,这将导致相同的原始错误:

store.collection('sessions').updateById(session._id.toString().trim(), {$set: status_obj} );