Experiment No:8: Title: Query Over Mongodb With A Collection
Experiment No:8: Title: Query Over Mongodb With A Collection
Experiment No:8: Title: Query Over Mongodb With A Collection
db.restaurants.find({},{"restaurant_id" : 1,"name":1,"borough":1,"address.zipcode" :
1,"_id":0});
2. Write a MongoDB query to display all the restaurant which is in the borough Bronx.
db.restaurants.find({"borough": "Bronx"});
3. Write a MongoDB query to find the restaurants which do not prepare any cuisine of
'American' and achieved a score more than 70 and located in the longitude less than
-65.754168.
Note : Do this query without using $and operator.
db.restaurants.find(
{
"cuisine" : {$ne : "American "},
"grades.score" :{$gt: 70},
"address.coord" : {$lt : -65.754168}
}
);
4. Write a MongoDB query to find the restaurants which do not prepare any cuisine of
'American ' and achieved a grade point 'A' not belongs to the borough Brooklyn. The
document must be displayed according to the cuisine in descending order.
db.restaurants.find( {
"cuisine" : {$ne : "American "},
"grades.grade" :"A",
"borough": {$ne : "Brooklyn"} }
).sort({"cuisine":-1});
5. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for
those
restaurants which achieved a score which is not more than 10.
db.restaurants.find(
{"grades.score" :
{ $not:
{$gt : 10}
}
},
{
"restaurant_id" : 1,
"name":1,"borough":1,
"cuisine" :1
}
);
6. Write a MongoDB query to find the restaurant Id, name, borough and cuisine for
those restaurants which prepared dish except 'American' and 'Chinees' or restaurant's
name
begins with letter 'Wil'.
db.restaurants.find(
{$or: [
{name: /^Wil/},
{"$and": [
{"cuisine" : {$ne :"American "}},
{"cuisine" : {$ne :"Chinees"}}
]}
]}
,{"restaurant_id" : 1,"name":1,"borough":1,"cuisine" :1}
);
7. Write a MongoDB query to know whether all the addresses contains the street or not.
db.restaurants.find(
{"address.street" :
{ $exists : true }
}
);
8. Write a MongoDB query which will select all documents in the restaurants collection
where the coord field value is Double.
db.restaurants.find(
{"address.coord" :
{$type : 1}
}
);
{
"id": "1",
"firstName": "Jane",
"lastName": "Doe",
"phoneNumber": "555-555-1212",
"email": "Jane.Doe@compose.io"
}
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
public class ConnectToDB {
public static void main( String args[] ) {
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("17131057", "Students",
"cse".toCharArray());
System.out.println("Connected to the database successfully");
// Accessing the database
MongoDatabase database = mongo.getDatabase("Students");
System.out.println("Credentials ::"+ credential);
}
}
Insrtion of 10 docs:
MongoClient mongo =
MongoClients.create("mongodb://127.0.0.1:27017");
MongoDatabase db = mongo.getDatabase("Students");
MongoCollection<document> collection =
db.getCollection("menuItem");
List<writemodel<document>> writes = new
ArrayList<writemodel<document>>(); // (1)
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud1")
.append("id", 1)
.append("firstName ", " Jane ")
.append("lastName "," Doe ")
.append( "phoneNumber", "555-555-1212",)
.append("email","Jane.Doe@compose.io")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud2")
.append("id", 2)
.append("firstName ", " Rushi ")
.append("lastName "," Gawane ")
.append( "phoneNumber", "8978734217",)
.append("email", "rushi123@gmail.com")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud3")
.append("id", 3)
.append("firstName ", " Sourabh ")
.append("lastName "," Mane")
.append( "phoneNumber",9876787677",)
.append("email", "sourbh123@gmail.com")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud4")
.append("id", 4)
.append("firstName ", " Raju ")
.append("lastName "," pawar")
.append( "phoneNumber","8767564321",)
.append("email", "rajuu123@gmail.com")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud5")
.append("id", 5)
.append("firstName ", " Nitin ")
.append("lastName "," Naikwadi ")
.append( "phoneNumber", "867787677",)
.append("email", "nitin123@gmail.com")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud6")
.append("id", 6)
.append("firstName ", " karan ")
.append("lastName “ Mohite ")
.append( "phoneNumber", "7876787677",)
.append("email", "karan1233@gmail.com")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud7")
.append("id", 7)
.append("firstName ", " Rushi ")
.append("lastName "," shinde")
.append( "phoneNumber", "9078787677",)
.append("email", "writes123@gmail.com")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud8")
.append("id", 8)
.append("firstName ", " Ranvir ")
.append("lastName “kapoor ")
.append( "phoneNumber","9578787656",)
.append("ranvir123@gmail.com")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud9")
.append("id", 9)
.append("firstName ", " priyanka ")
.append("lastName "," chopra ")
.append( "phoneNumber", "8978787677",)
.append("priygmai123@gmail.com")
)
);
writes.add(
new InsertOneModel<document>(
new Document("menuItem", "stud10")
.append("id", 10)
.append("firstName ", " dipika ")
.append("lastName "," padukon")
.append( "phoneNumber", "7878787698",)
.append("dips123@gmail.com")
)
);
2. Read collection -
DBCursor cursor = menuItem.find();
3. Update collection -
BasicDBObject findTestItemQuery = new BasicDBObject();
findTestItemQuery.put("firstName",Rushi");
DBCursor testItemsCursor = menuItem.find(findTestItemQuery);
if(testItemsCursor.hasNext()) {
DBObject testCodeItem = testItemsCursor.next();
testCodeItem.put("name", "Pravin");
menuItem.save(testCodeItem);
}
4. Delete collection -
BasicDBObject deleteQuery = new BasicDBObject();
deleteQuery.put("firstName", "Pravin");
DBCursor cursor = menuItem.find(deleteQuery);
while (cursor.hasNext()) {
DBObject item = cursor.next();
menuItem.remove(menuItem);
}