How to extract particular array value into a new field using $project?

‘countries’ is an array in documents and contains list of country names.I want 2nd element of ‘countries’ array in new field ‘newCountry’, but i am not getting any element in this new field on running the query.
I entered -
db.movies.aggregate([{$project:{_id:0, newCountry: “$countries.1”}}])
Output -
{ “newCountry” : } { “newCountry” : } { “newCountry” : } { “newCountry” : } { “newCountry” : } { “newCountry” : } { “newCountry” : } { “newCountry” : }

I also tried “$countries.0” but still getting empty results

Let’s see a screenshot of a pretty() version of some countries documents.

Is this related to any lab? If yes, which one?

No its not related to any lab. I was exploring different stages and operators in aggregation when I came across this scenario.

I think we need to use $map and $arrayElemAt operators to accomplish this task.

You want it to be an array with one element or a string?

I want it as a string element.

Ok, $arrayElemAt is all you need:
$project: { countries: {$arrayElemAt: ["$countries", 0]} }

You can use the $slice operator

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.aggregate([{$match:{countries:{$elemMatch:{$exists:true}}}},{$project:{secondElemCountry:{$size:["$countries"]},countries:1}},{$match:{secondElemCountry:{$gte:2}}},{$project:{secondElemCountry:{$arrayElemAt:["$countries",1]}}}])
{ “_id” : ObjectId(“573a1390f29313caabcd432a”), “secondElemCountry” : “France” }
{ “_id” : ObjectId(“573a1390f29313caabcd4ad2”), “secondElemCountry” : “USA” }
{ “_id” : ObjectId(“573a1391f29313caabcd7586”), “secondElemCountry” : “France” }
{ “_id” : ObjectId(“573a1391f29313caabcd7c9d”), “secondElemCountry” : “Austria” }
{ “_id” : ObjectId(“573a1391f29313caabcd8076”), “secondElemCountry” : “Germany” }
{ “_id” : ObjectId(“573a1391f29313caabcd8972”), “secondElemCountry” : “Germany” }
{ “_id” : ObjectId(“573a1391f29313caabcd9263”), “secondElemCountry” : “UK” }
{ “_id” : ObjectId(“573a1391f29313caabcd9721”), “secondElemCountry” : “France” }
{ “_id” : ObjectId(“573a1392f29313caabcd97ee”), “secondElemCountry” : “USA” }
{ “_id” : ObjectId(“573a1392f29313caabcd9912”), “secondElemCountry” : “USA” }
{ “_id” : ObjectId(“573a1392f29313caabcd9d67”), “secondElemCountry” : “France” }
{ “_id” : ObjectId(“573a1392f29313caabcd9ea5”), “secondElemCountry” : “UK” }
{ “_id” : ObjectId(“573a1392f29313caabcda0bb”), “secondElemCountry” : “USA” }
{ “_id” : ObjectId(“573a1392f29313caabcda110”), “secondElemCountry” : “Austria” }
{ “_id” : ObjectId(“573a1392f29313caabcda158”), “secondElemCountry” : “France” }
{ “_id” : ObjectId(“573a1392f29313caabcda18c”), “secondElemCountry” : “Germany” }
{ “_id” : ObjectId(“573a1392f29313caabcda8c3”), “secondElemCountry” : “Germany” }
{ “_id” : ObjectId(“573a1392f29313caabcdb22d”), “secondElemCountry” : “Germany” }
{ “_id” : ObjectId(“573a1392f29313caabcdb975”), “secondElemCountry” : “Germany” }
{ “_id” : ObjectId(“573a1393f29313caabcdbeca”), “secondElemCountry” : “Spain” }
Type “it” for more

tunning it;
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.aggregate([{$match:{countries:{$elemMatch:{$exists:true}}}},{$project:{secondElemCountry:{$size:["$countries"]},countries:1}},{$match:{secondElemCountry:{$gte:2}}},{$project:{secondElemCountry:{$arrayElemAt:["$countries",1]}}}]).itcount()
8989
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movies.aggregate([{$match:{countries:{$elemMatch:{$exists:true}}}},{$project:{secondElemCountry:[{$arrayElemAt:["$countries",1]}]}},{$match:{secondElemCountry:{$ne:[null]}}}]).itcount()
8989