数据事例:
> 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" ] }
?