【问题标题】:Exception : can't convert from BSON type OID to String例外:无法从 BSON 类型 OID 转换为字符串
【发布时间】:2013-01-25 21:14:48
【问题描述】:

我是 mongo db 的新手,所以这是我在尝试通过控制台在 mongodb 中将名称更改为大写时遇到以下错误的问题。 这是查询:

t.aggregate([{$project:{name:{$toUpper:"$_id"} , _id:0}}])

另外,我手动插入了所有以“_id”为名称的字段,例如 “_id”:“乔”

提前致谢

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您的语法看起来是正确的,因此您应该尝试查找不是字符串的文档。在 shell 中输入:

    db.t.find({}).forEach(function(item) { if(typeof item._id !== 'string') { print(item._id); })
    

    这将输出任何不是字符串的 ID。

    【讨论】:

    • 仅供参考——基本思想相同——您也可以在查询中使用 $type 运算符: t.find( { _id: {$not: {$type: 2 } } } ) // 其中类型 2 == 字符串,类型 7 是 ObjectId
    猜你喜欢
    • 2017-11-04
    • 2015-04-09
    • 2021-03-22
    • 2017-09-23
    • 2018-05-24
    • 1970-01-01
    • 2020-11-09
    • 2018-03-20
    相关资源
    最近更新 更多