search

Home  >  Q&A  >  body text

mongodb - mongo中如何在添加一个字段的时候,指定他的字段类型。

比如我想增加一个is_delete 字段,想指定类型为int_32,并且默认值为0.
没有找到好办法
我现在做的是添加这个字段,值为1,然后类型就自动为int_32,然后在批量update为0.
如果直接为0.他显示的是bool false

有没有更简单方法呢。

天蓬老师天蓬老师2793 days ago614

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-28 09:06:23

    It doesn’t seem to work, I haven’t seen a way to set it at the database level. For NOSQL like MongoDB, you have to use ORM or business code to deal with this problem.

    As for what you said, it shouldn’t show bool false after update, right?

    > db.test.save({'a':1,'b':1})
    WriteResult({ "nInserted" : 1 })
    > db.test.find()
    { "_id" : ObjectId("5625f1d9d782d26c6536ee8c"), "a" : 1, "b" : 1 }
    > db.test.update({'a':1},{'$set':{'b':0}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.test.find({'a':1})
    { "_id" : ObjectId("5625f1d9d782d26c6536ee8c"), "a" : 1, "b" : 0 }
    >

    Isn’t this still 0?

    reply
    0
  • Cancelreply