java往mongoDB写入资料时如何指定数据类型,以避免数据类型为null?
-
我用java往mongoDB写资料时,如果某个栏位写入的资料为null,那之后查询该栏位的资料类型也为null。能不能通过写入资料时指定数据类型,来避免这种情况呢?
-
建立collection时,设定验证字段格式,
db.createCollection( "contacts", { validator: { $and: [ { phone: { $type: "string" } }, { email: { $regex: /@mongodb\.com$/ } }, { status: { $in: [ "Unknown", "Incomplete" ] } } ] } } )
或是在现有的collection,在使用以下指令设定。当插入数据时便会验证是否符合格式
db.runCommand( { collMod: "contacts", --collection名 validator: { $and: [ { phone: { $exists: true } }, { email: { $exists: true } } ] }, validationLevel: "moderate" } )
Mongodb官網說明:
本帖下載内容已隐藏,请登入以查看隐藏内容!