Saturday 5 June 2021

Mongo shell: Convert find query results to an array

‘find().toArray()’ method converts the find() query results to an array. Since it loads all the matched documents into the memory, use toArray() method with caution.

> db.countryCapitals.find().toArray()
[
    {
        "_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"
    },
    {
        "_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"
    }
]

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment