Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

How to select a record from an array in MongoDB collection

I'm new to MongoDB.

Just created a collection named as datasets and inserted one big json in it.

Document :

{
   "_id":ObjectId(   "5e1f2b248eebbe6d289d53c6"   ),
   "countries":{
      "country":[
         {
            "area":9596960,
            "cities":{
               "city":[
                  {
                     "citypopulation":7830000,
                     "cityname":"Shanghai"
                  },
                  {
                     "citypopulation":7000000,
                     "cityname":"Beijing"
                  },
                  {
                     "citypopulation":6218000,
                     "cityname":"HongKong"
                  },
                  {
                     "citypopulation":5770000,
                     "cityname":"Tianjin"
                  },
                  {
                     "citypopulation":4540000,
                     "cityname":"Shenyang"
                  },
                  {
                     "citypopulation":3750000,
                     "cityname":"Wuhan"
                  },
                  {
                     "citypopulation":3580000,
                     "cityname":"Guangzhou"
                  },
                  {
                     "citypopulation":2980000,
                     "cityname":"Chongqing"
                  },
                  {
                     "citypopulation":2830000,
                     "cityname":"Harbin"
                  },
                  {
                     "citypopulation":2810000,
                     "cityname":"Chengdu"
                  },
                  {
                     "citypopulation":2760000,
                     "cityname":"Xian"
                  },
                  {
                     "citypopulation":2500000,
                     "cityname":"Nanjing"
                  },
                  {
                     "citypopulation":2460000,
                     "cityname":"Zibo"
                  },
                  {
                     "citypopulation":2400000,
                     "cityname":"Dalian"
                  },
                  {
                     "citypopulation":2320000,
                     "cityname":"Jinan"
                  },
                  {
                     "citypopulation":2110000,
                     "cityname":"Changchun"
                  },
                  {
                     "citypopulation":2060000,
                     "cityname":"Qingdao"
                  },
                  {
                     "citypopulation":1960000,
                     "cityname":"Taiyuan"
                  },
                  {
                     "citypopulation":1710000,
                     "cityname":"Zhengzhou"
                  },
                  {
                     "citypopulation":1530000,
                     "cityname":"Guiyang"
                  },
                  {
                     "citypopulation":1520000,
                     "cityname":"Kunming"
                  },
                  {
                     "citypopulation":1510000,
                     "cityname":"Lanzhou"
                  },
                  {
                     "citypopulation":1500000,
                     "cityname":"Tangshan"
                  },
                  {
                     "citypopulation":1390000,
                     "cityname":"Anshan"
                  },
                  {
                     "citypopulation":1380000,
                     "cityname":"Qiqihar"
                  },
                  {
                     "citypopulation":1350000,
                     "cityname":"Nanchang"
                  },
                  {
                     "citypopulation":1350000,
                     "cityname":"Fushun"
                  },
                  {
                     "citypopulation":1340000,
                     "cityname":"Hangzhou"
                  },
                  {
                     "citypopulation":1330000,
                     "cityname":"Changsha"
                  },
                  {
                     "citypopulation":1320000,
                     "cityname":"Shijiazhuang"
                  },
                  {
                     "citypopulation":1290000,
                     "cityname":"Fuzhou"
                  },
                  {
                     "citypopulation":1270000,
                     "cityname":"Jilin"
                  },
                  {
                     "citypopulation":1200000,
                     "cityname":"Huainan"
                  },
                  {
                     "citypopulation":1200000,
                     "cityname":"Baotou"
                  },
                  {
                     "citypopulation":1190000,
                     "cityname":"Luoyang"
                  },
                  {
                     "citypopulation":1160000,
                     "cityname":"Urumqi"
                  },
                  {
                     "citypopulation":1110000,
                     "cityname":"Handan"
                  },
                  {
                     "citypopulation":1110000,
                     "cityname":"Datong"
                  },
                  {
                     "citypopulation":1090000,
                     "cityname":"Ningbo"
                  },
                  {
                     "citypopulation":1070000,
                     "cityname":"Nanning"
                  },
                  {
                     "citypopulation":1000000,
                     "cityname":"Hefei"
                  }
               ]
            },
            "name":"China",
            "population":1210004992
         },
         {
            "area":3287590,
            "cities":{
               "city":[
                  {
                     "citypopulation":9925891,
                     "cityname":"Mumbai"
                  },
                  {
                     "citypopulation":7206704,
                     "cityname":"NewDelhi"
                  },
                  {
                     "citypopulation":4399819,
                     "cityname":"Calcutta"
                  },
                  {
                     "citypopulation":3841396,
                     "cityname":"Madras"
                  },
                  {
                     "citypopulation":3302296,
                     "cityname":"Bangalore"
                  },
                  {
                     "citypopulation":3145939,
                     "cityname":"Hyderabad"
                  },
                  {
                     "citypopulation":2954526,
                     "cityname":"Ahmadabad"
                  },
                  {
                     "citypopulation":1879420,
                     "cityname":"Kanpur"
                  },
                  {
                     "citypopulation":1624752,
                     "cityname":"Nagpur"
                  },
                  {
                     "citypopulation":1619115,
                     "cityname":"Lucknow"
                  },
                  {
                     "citypopulation":1566651,
                     "cityname":"Pune"
                  },
                  {
                     "citypopulation":1505872,
                     "cityname":"Surat"
                  },
                  {
                     "citypopulation":1458183,
                     "cityname":"Jaipur"
                  },
                  {
                     "citypopulation":1091674,
                     "cityname":"Indore"
                  },
                  {
                     "citypopulation":1062771,
                     "cityname":"Bhopal"
                  },
                  {
                     "citypopulation":1061598,
                     "cityname":"Vadodara"
                  },
                  {
                     "citypopulation":1042740,
                     "cityname":"Ludhiana"
                  },
                  {
                     "citypopulation":1014557,
                     "cityname":"Kalyan"
                  }
               ]
            },
            "name":"India",
            "population":952107712
         }
      ]
   }
}

I want to know, What types of find operations I can do to select this one json entry for learning purpose..?

I was trying to run below find operation, nothing comes out in result.

db.datasets.find({'name':'China'})

Secondly, I've added above json in datasets collections using below command

mongoimport --db admin --collection datasets --file /home/lenny/TransformedJSON.json

Its working fine, but I want to fail this command. Becasue I dont want to insert without username and password. How it can be done ?

Can anyone please suggest ..?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$elemMatch operator is used to filter values in an array.

db.getCollection("datasets").find({
        'countries.country': {
            $elemMatch: {
                name: 'China'
            }
        }
    }, {
        'countries.country.$': 1
    })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...