MongoDB query to find a specific city record from a collection

MongoDBDatabaseBig Data Analytics

For this, use find() and fetch a specific record. Let us create a collection with documents −

> db.demo30.insertOne({"City":"New York"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e174ceccfb11e5c34d898bd")
}
> db.demo30.insertOne({"City":"Los Angeles"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e174d0ecfb11e5c34d898be")
}
> db.demo30.insertOne({"City":"Chicago"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e174d18cfb11e5c34d898bf")
}
> db.demo30.insertOne({"City":"Los Angeles"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e174d1dcfb11e5c34d898c0")
}

Display all documents from a collection with the help of find() method −

> db.demo30.find();

This will produce the following output −

{ "_id" : ObjectId("5e174ceccfb11e5c34d898bd"), "City" : "New York" }
{ "_id" : ObjectId("5e174d0ecfb11e5c34d898be"), "City" : "Los Angeles" }
{ "_id" : ObjectId("5e174d18cfb11e5c34d898bf"), "City" : "Chicago" }
{ "_id" : ObjectId("5e174d1dcfb11e5c34d898c0"), "City" : "Los Angeles" }

Here is the query to find a specific city −

> db.demo30.find({"City":"Chicago"});

This will produce the following output −

{ "_id" : ObjectId("5e174d18cfb11e5c34d898bf"), "City" : "Chicago" }
raja
Published on 02-Apr-2020 16:05:42
Advertisements