‘find()’ method return a cursor object, which is used to iterate over the results. By default, you will get 20 results max.
Let’s see it with an example.
countryCapitals.json
[
{
"country": "Afghanistan",
"city": "Kabul"
},
{
"country": "Albania",
"city": "Tirana"
},
{
"country": "Algeria",
"city": "Alger"
},
{
"country": "American Samoa",
"city": "Fagatogo"
},
{
"country": "Andorra",
"city": "Andorra la Vella"
},
{
"country": "Angola",
"city": "Luanda"
},
{
"country": "Anguilla",
"city": "The Valley"
},
{
"country": "Antarctica",
"city": null
},
{
"country": "Antigua and Barbuda",
"city": "Saint John's"
},
{
"country": "Argentina",
"city": "Buenos Aires"
},
{
"country": "Armenia",
"city": "Yerevan"
},
{
"country": "Aruba",
"city": "Oranjestad"
},
{
"country": "Australia",
"city": "Canberra"
},
{
"country": "Austria",
"city": "Wien"
},
{
"country": "Azerbaijan",
"city": "Baku"
},
{
"country": "Bahamas",
"city": "Nassau"
},
{
"country": "Bahrain",
"city": "al-Manama"
},
{
"country": "Bangladesh",
"city": "Dhaka"
},
{
"country": "Barbados",
"city": "Bridgetown"
},
{
"country": "Belarus",
"city": "Minsk"
},
{
"country": "Belgium",
"city": "Bruxelles [Brussel]"
},
{
"country": "Belize",
"city": "Belmopan"
},
{
"country": "Benin",
"city": "Porto-Novo"
},
{
"country": "Bermuda",
"city": "Hamilton"
},
{
"country": "Bhutan",
"city": "Thimphu"
},
{
"country": "Bolivia",
"city": "La Paz"
},
{
"country": "Bosnia and Herzegovina",
"city": "Sarajevo"
},
{
"country": "Botswana",
"city": "Gaborone"
}
]
Let’s insert above document into countryCapitals collection.
> db.countryCapitals.insert( ... [ ... { ... "country": "Afghanistan", ... "city": "Kabul" ... }, ... { ... "country": "Albania", ... "city": "Tirana" ... }, ... { ... "country": "Algeria", ... "city": "Alger" ... }, ... { ... "country": "American Samoa", ... "city": "Fagatogo" ... }, ... { ... "country": "Andorra", ... "city": "Andorra la Vella" ... }, ... { ... "country": "Angola", ... "city": "Luanda" ... }, ... { ... "country": "Anguilla", ... "city": "The Valley" ... }, ... { ... "country": "Antarctica", ... "city": null ... }, ... { ... "country": "Antigua and Barbuda", ... "city": "Saint John's" ... }, ... { ... "country": "Argentina", ... "city": "Buenos Aires" ... }, ... { ... "country": "Armenia", ... "city": "Yerevan" ... }, ... { ... "country": "Aruba", ... "city": "Oranjestad" ... }, ... { ... "country": "Australia", ... "city": "Canberra" ... }, ... { ... "country": "Austria", ... "city": "Wien" ... }, ... { ... "country": "Azerbaijan", ... "city": "Baku" ... }, ... { ... "country": "Bahamas", ... "city": "Nassau" ... }, ... { ... "country": "Bahrain", ... "city": "al-Manama" ... }, ... { ... "country": "Bangladesh", ... "city": "Dhaka" ... }, ... { ... "country": "Barbados", ... "city": "Bridgetown" ... }, ... { ... "country": "Belarus", ... "city": "Minsk" ... }, ... { ... "country": "Belgium", ... "city": "Bruxelles [Brussel]" ... }, ... { ... "country": "Belize", ... "city": "Belmopan" ... }, ... { ... "country": "Benin", ... "city": "Porto-Novo" ... }, ... { ... "country": "Bermuda", ... "city": "Hamilton" ... }, ... { ... "country": "Bhutan", ... "city": "Thimphu" ... }, ... { ... "country": "Bolivia", ... "city": "La Paz" ... }, ... { ... "country": "Bosnia and Herzegovina", ... "city": "Sarajevo" ... }, ... { ... "country": "Botswana", ... "city": "Gaborone" ... } ... ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 28, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
db.collection.find() in the mongo shell automatically iterates the cursor to display up to the first 20 documents. Type 'it' to continue to get next batch results.
> db.countryCapitals.find() { "_id" : ObjectId("60bbadbe8251689f64fb3ed2"), "country" : "Afghanistan", "city" : "Kabul" } { "_id" : ObjectId("60bbadbe8251689f64fb3ed3"), "country" : "Albania", "city" : "Tirana" } { "_id" : ObjectId("60bbadbe8251689f64fb3ed4"), "country" : "Algeria", "city" : "Alger" } { "_id" : ObjectId("60bbadbe8251689f64fb3ed5"), "country" : "American Samoa", "city" : "Fagatogo" } { "_id" : ObjectId("60bbadbe8251689f64fb3ed6"), "country" : "Andorra", "city" : "Andorra la Vella" } { "_id" : ObjectId("60bbadbe8251689f64fb3ed7"), "country" : "Angola", "city" : "Luanda" } { "_id" : ObjectId("60bbadbe8251689f64fb3ed8"), "country" : "Anguilla", "city" : "The Valley" } { "_id" : ObjectId("60bbadbe8251689f64fb3ed9"), "country" : "Antarctica", "city" : null } { "_id" : ObjectId("60bbadbe8251689f64fb3eda"), "country" : "Antigua and Barbuda", "city" : "Saint John's" } { "_id" : ObjectId("60bbadbe8251689f64fb3edb"), "country" : "Argentina", "city" : "Buenos Aires" } { "_id" : ObjectId("60bbadbe8251689f64fb3edc"), "country" : "Armenia", "city" : "Yerevan" } { "_id" : ObjectId("60bbadbe8251689f64fb3edd"), "country" : "Aruba", "city" : "Oranjestad" } { "_id" : ObjectId("60bbadbe8251689f64fb3ede"), "country" : "Australia", "city" : "Canberra" } { "_id" : ObjectId("60bbadbe8251689f64fb3edf"), "country" : "Austria", "city" : "Wien" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee0"), "country" : "Azerbaijan", "city" : "Baku" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee1"), "country" : "Bahamas", "city" : "Nassau" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee2"), "country" : "Bahrain", "city" : "al-Manama" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee3"), "country" : "Bangladesh", "city" : "Dhaka" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee4"), "country" : "Barbados", "city" : "Bridgetown" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee5"), "country" : "Belarus", "city" : "Minsk" } Type "it" for more
Type"it" to get next 20 documents.
> it { "_id" : ObjectId("60bbadbe8251689f64fb3ee6"), "country" : "Belgium", "city" : "Bruxelles [Brussel]" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee7"), "country" : "Belize", "city" : "Belmopan" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee8"), "country" : "Benin", "city" : "Porto-Novo" } { "_id" : ObjectId("60bbadbe8251689f64fb3ee9"), "country" : "Bermuda", "city" : "Hamilton" } { "_id" : ObjectId("60bbadbe8251689f64fb3eea"), "country" : "Bhutan", "city" : "Thimphu" } { "_id" : ObjectId("60bbadbe8251689f64fb3eeb"), "country" : "Bolivia", "city" : "La Paz" } { "_id" : ObjectId("60bbadbe8251689f64fb3eec"), "country" : "Bosnia and Herzegovina", "city" : "Sarajevo" } { "_id" : ObjectId("60bbadbe8251689f64fb3eed"), "country" : "Botswana", "city" : "Gaborone" } >
No comments:
Post a Comment