You are on page 1of 10

CIA3-selection query

Name: Soumya a

Class: MCA Regular(Electives)lateral.

USN:

1) Retrieve all documents from the user collection where status equals either
“P” or ”D”.
2) Retrieve all documents from the user collection where status equals”A”
and age is less than 30.
3) Retrieve all documents from the user collection where status equals”A”
or age is less than 30.
4) Find the compound query that collects all documents where status equal
“A”and either age is less than 30 or type equals 1.
5) Find all documents where the finished array contains atleast one element
that is grater than 15 and less than 20.
6) Write a query that selects all documents all documents where the points is
an array with atleast 1 embeded document that contain field points whose
value is less than or equals to 55.
7) Find queries for documents where the points array has at least 1 embeded
document that contain both the field points less than or equal to 70 and
the field bonus equals to 20.
8) Find the queries for documents where 1 element that satisfies the points
less than or equal to 70 condition and other element satisfies the bonus
equal to 20 condition ,or a single element both criteria.

Document structure
db.users.insertMany(

_id: 1,

name: "sue",

age: 19,

type: 1,

status: "P",

favorites: { artist: "Picasso", food: "pizza" },

finished: [ 17, 3 ],

badges: [ "blue", "black" ],


points: [

{ points: 85, bonus: 20 },

{ points: 85, bonus: 10 }

},

_id: 2,

name: "bob",

age: 42,

type: 1,

status: "A",

favorites: { artist: "Miro", food: "meringue" },

finished: [ 11, 25 ],

badges: [ "green" ],

points: [

{ points: 85, bonus: 20 },

{ points: 64, bonus: 12 }

},

_id: 3,

name: "ahn",

age: 22,

type: 2,

status: "A",

favorites: { artist: "Cassatt", food: "cake" },

finished: [ 6 ],

badges: [ "blue", "red" ],

points: [

{ points: 81, bonus: 8 },

{ points: 55, bonus: 20 }

},

{
_id: 4,

name: "xi",

age: 34,

type: 2,

status: "D",

favorites: { artist: "Chagall", food: "chocolate" },

finished: [ 5, 11 ],

badges: [ "red", "black" ],

points: [

{ points: 53, bonus: 15 },

{ points: 51, bonus: 15 }

},

_id: 5,

name: "xyz",

age: 23,

type: 2,

status: "D",

favorites: { artist: "Noguchi", food: "nougat" },

finished: [ 14, 6 ],

badges: [ "orange" ],

points: [

{ points: 71, bonus: 20 }

},

_id: 6,

name: "abc",

age: 43,

type: 1,

status: "A",

favorites: { food: "pizza", artist: "Picasso" },

finished: [ 18, 12 ],
badges: [ "black", "blue" ],

points: [

{ points: 78, bonus: 8 },

{ points: 57, bonus: 7 }

Outputs:
1)db.users.find({status:{$in:[“P”,”D”]}}).pretty();

2)db.users.find({status:”A”,age:{$lt:30}}).pretty();
3)db.users.find({$or:[{status:”A”},{age:{$lt:30}}]}).pretty();

4)db.users.find({status:”A”,$or:[{age:{$lt:30}},{type:1}]}).pretty();

5)db.user.find({finished:{$gt:15,$lt:20}}).pretty();
6)db.user.find({“points.points”:{$lte:55}}).pretty();

8) db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } ).pretty();


CIA4-projection

Name: Soumya a

Class: MCA Regular(Electives)lateral.

USN:

1) Find out select name and status from user collection where user collection
where user status is “A”.
2) Find out select name and status from user collection where user collection
where user status is “A” and without id field.
3) Select all fields except favorite and points from user collection where
points field has points value less than 20.
4) Select name status favorite food from user collection where user has
and ,or black badge.
5) Select name , status and last entry for points field where status=A in user
collection.
Document structure
db.users.insertMany(

_id: 1,

name: "sue",

age: 19,

type: 1,

status: "P",

favorites: { artist: "Picasso", food: "pizza" },

finished: [ 17, 3 ],

badges: [ "blue", "black" ],

points: [

{ points: 85, bonus: 20 },

{ points: 85, bonus: 10 }

},

_id: 2,

name: "bob",

age: 42,
type: 1,

status: "A",

favorites: { artist: "Miro", food: "meringue" },

finished: [ 11, 25 ],

badges: [ "green" ],

points: [

{ points: 85, bonus: 20 },

{ points: 64, bonus: 12 }

},

_id: 3,

name: "ahn",

age: 22,

type: 2,

status: "A",

favorites: { artist: "Cassatt", food: "cake" },

finished: [ 6 ],

badges: [ "blue", "red" ],

points: [

{ points: 81, bonus: 8 },

{ points: 55, bonus: 20 }

},

_id: 4,

name: "xi",

age: 34,

type: 2,

status: "D",

favorites: { artist: "Chagall", food: "chocolate" },

finished: [ 5, 11 ],

badges: [ "red", "black" ],

points: [
{ points: 53, bonus: 15 },

{ points: 51, bonus: 15 }

},

_id: 5,

name: "xyz",

age: 23,

type: 2,

status: "D",

favorites: { artist: "Noguchi", food: "nougat" },

finished: [ 14, 6 ],

badges: [ "orange" ],

points: [

{ points: 71, bonus: 20 }

},

_id: 6,

name: "abc",

age: 43,

type: 1,

status: "A",

favorites: { food: "pizza", artist: "Picasso" },

finished: [ 18, 12 ],

badges: [ "black", "blue" ],

points: [

{ points: 78, bonus: 8 },

{ points: 57, bonus: 7 }

)
Outputs:
1) db.users.find( { status: "A" }, { name: 1, status: 1 } );

2) db.users.find( { status: "A" }, { name: 1, status: 1, _id: 0 });

3) db.users.find( { “points.points”:{$lt:20} }, { favorites: 0, points: 0 } )


4) s
5) db.users.find( { status: "A" }, { name: 1, status: 1, points: { $slice: -1 } } )

You might also like