Mongo shell 函数详解 - 数组_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Mongo shell 函数详解 - 数组

Mongo shell 函数详解 - 数组

 2014/9/23 23:10:44  晨必须的  程序员俱乐部  我要评论(0)
  • 摘要:数据事例:>db.language.find(){"_id":1,"name":"java","tags":["jsp","servlet","ejb"]}{"_id":2,"name":"db","tags":["mysql","mongo"]}{"_id":3,"name":".net","tags":["asp"]}{"_id":4,"name":"javascript","tags":[]}{"_id":5,"name":"ruby"}>vartags=db
  • 标签:详解 函数 数组

数据事例:

> db.language.find()

{ "_id" : 1, "name" : "java", "tags" : [ "jsp", "servlet", "ejb" ] }

{ "_id" : 2, "name" : "db", "tags" : [ "mysql", "mongo" ] }

{ "_id" : 3, "name" : ".net", "tags" : [ "asp" ] }

{ "_id" : 4, "name" : "javascript", "tags" : [ ] }

{ "_id" : 5, "name" : "ruby" }

?

> var tags = db.language.findOne({_id:1}).tags

> Array.isArray(tags)

true

> Array.contains(tags,"jsp")

true

> Array.contains(tags,"jdbc")

false

> tags

[ "jsp", "servlet", "ejb" ]

> tags[3]="jsp"

jsp

> tags

[ "jsp", "servlet", "ejb", "jsp" ]

> tags = Array.unique(tags)

[ "jsp", "servlet", "ejb" ]

> Array.shuffle(tags)

[ "jsp", "ejb", "servlet" ]

> Array.shuffle(tags)

[ "servlet", "ejb", "jsp" ]

> Array.shuffle(tags)

[ "jsp", "servlet", "ejb" ]

> Object.keySet(tags)

[ "0", "1", "2" ]

> db.language.find({tags:{$size:0}})

{ "_id" : 4, "name" : "javascript", "tags" : [ ] }

> db.language.find(function(){return !Array.isArray(this.tags)})

{ "_id" : 5, "name" : "ruby" }

> db.language.find(function(){return Array.isArray(this.tags)})

{ "_id" : 1, "name" : "java", "tags" : [ "jsp", "servlet", "ejb" ] }

{ "_id" : 2, "name" : "db", "tags" : [ "mysql", "mongo" ] }

{ "_id" : 3, "name" : ".net", "tags" : [ "asp" ] }

{ "_id" : 4, "name" : "javascript", "tags" : [ ] }

> db.language.find(function(){return (Array.isArray(this.tags) && this.tags.length >= 3)})

{ "_id" : 1, "name" : "java", "tags" : [ "jsp", "servlet", "ejb" ] }

?

?

?

> var allTags = new Array();

> var langs = db.language.find(function(){return Array.isArray(this.tags) && this.tags.length > 0}).toArray();

> for(var i=0;i<langs.length;i++){

... var tags = langs[i].tags;

... for(var j in tags){

... allTags.push(tags[j]);

... }

... }

6

> allTags

[ "jsp", "servlet", "ejb", "mysql", "mongo", "asp" ]

>?

?

> db.language.find({"tags.0":"jsp"})

{ "_id" : 1, "name" : "java", "tags" : [ "jsp", "servlet", "ejb" ] }

?

  • class='magplus' title='点击查看原始大小图片' />
  • 描述: 数组
  • 大小: 146 KB
  • 大小: 55.1 KB
  • 大小: 77.9 KB
  • 查看图片附件
发表评论
用户名: 匿名