香港云主机最佳企业级服务商!

ADSL拨号VPS包含了中国大陆(联通,移动,电信,)

中国香港,国外拨号VPS。

当前位置:云主机 > Mongodb >

电信ADSL拨号VPS
联通ADSL拨号VPS
移动ADSL拨号VPS

Mongodb 利用mongoshell进行数据类型转换的实现方法


时间:2020-11-14 13:38 作者:admin


$type操作符

检测类型

种类 代号 别名

Double 1 “double”
String 2 “string”
Object 3 “object”
Array 4 “array”
Binary data 5 “binData”
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”
Boolean 8 “bool”
Date 9 “date”
Null 10 “null”
Regular Expression 11 “regex”
DBPointer 12 “dbPointer”
JavaScript 13 “javascript”
Symbol 14 “symbol”
JavaScript (with scope) 15 “javascriptWithScope”
32-bit integer 16 “int”
Timestamp 17 “timestamp”
64-bit integer 18 “long”
Min key -1 “minKey”
Max key 127 “maxKey

db.article.find({data:{$type:2}) //寻找data字段为string的文档

forEach函数

对查询结果集合中每个文档使用js函数

cursor.forEach(function)Iterates the cursor to apply a JavaScript function to each document from the cursor.

使用例子

将data.tagList数组中的string转换为int32,x代表迭代传入的文档

db.article.find({"data.tagList.0":{$type:2}}).forEach(function(x){var i=0;var length=x.data.tagList.length; for(i=0;i<length;i++ ){  if(typeof x.data.tagList[i] === 'string') {  x.data.tagList[i]=NumberInt(x.data.tagList[i]);  } };db.article.save(x)})

note

1.使用js新特性要注意,比如我的是不支持for(var a of b)的,还有注意string是小写啊

2.可以使用print输出

db.users.find().forEach( function(myDoc) { print( "user: " + myDoc.name ); } );

以上这篇Mongodb 利用mongoshell进行数据类型转换就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

(责任编辑:admin)






帮助中心
会员注册
找回密码
新闻中心
快捷通道
域名登录面板
虚机登录面板
云主机登录面板
关于我们
关于我们
联系我们
联系方式

售前咨询:17830004266(重庆移动)

企业QQ:383546523

《中华人民共和国工业和信息化部》 编号:ICP备00012341号

Copyright © 2002 -2018 香港云主机 版权所有
声明:香港云主机品牌标志、品牌吉祥物均已注册商标,版权所有,窃用必究

云官方微信

在线客服

  • 企业QQ: 点击这里给我发消息
  • 技术支持:383546523

  • 公司总台电话:17830004266(重庆移动)
  • 售前咨询热线:17830004266(重庆移动)