Null: Special value to denote fields without any value
{audience_rating: 6}
{audience_rating: 7.6}
{"title": "A Swedish Love Story", released: "1970-04-24"}
{"title": "A Swedish Love Story", released: "24-04-1970"}
{"title": "A Swedish Love Story", released: "24th April 1970"}
{"title": "A Swedish Love Story", released: "Fri, 24 Apr 1970"}
Variables
var name="Rachel Green"
name
var plainNum = 1299
plainNum
// forces MongoDB to use 32 bit
var explicitInt = NumberInt("1299")
explicitInt
// long forces 64 bit
var explicitLong = NumberLong("777888222116643")
explicitLong
var explicitLong_double = NumberLong(444333222111242)
explicitLong_double
// forces 128 bit
var explicitDecimal = NumberDecimal("142.42")
explicitDecimal
var explicitDecimal_double = NumberDecimal(142.42)
explicitDecimal_double
//values are rounded off
var decDbl = NumberDecimal(5999999999.99999999)
decDbl
// boolean demo
var isActive=true
isActive
var isDeleted=false
isDeleted
// objects
var friend={
"first_name":"Rachel",
"last_name":"Green",
"email":"rachel@friends.com"
}
friend
friend.first_name
var friends1=[
{
"firstname": "Phoebe",
"lastname":"Buffay",
"age": 31,
"profession":"Therapist"
},
{
"firstname": "Ross",
"lastname":"Geller",
"age": 31,
"location": "NY",
"profession":"Palentologist",
"spouses":["Carol","Emily","Rachel"]
},
{
"firstname": "Chandler",
"lastname":"Bing",
"age": 31,
"location": "NY"
},
{
"firstname": "Joey",
"lastname":" Tribianni",
"age": 32,
"location": "NYC",
"profession":"actor"
}
]
var friends2 = [
{
"firstname": "rachel",
"lastname" : "green",
"age" : 30,
"location": "NYC",
"profession":"Fashion Designer"
},
{
"name":{"firstname":"Ben",
"lastname" : "Geller"},
"age" : 6,
"location": "NYC"
},
{
"name":{"firstname":"Emma",
"lastname" : "Geller"},
"age" : 1,
"location": "NYC"
}
]
MongoDB projections specify which fields should be returned in the documents that match a query. All fields are returned by default, but you can include or exclude specific fields to control the amount of data MongoDB returns.
Projections can help improve performance by reducing network bandwidth and processing time, especially for documents with large amounts of data or when you only need a subset.
Return firstname and lastname only
This query tells MongoDB to return only the firstname and profession fields of all documents in the friends1 collection. If you want to exclude the _id field as well, you can do so by explicitly setting it to 0 in the projection:
db.friendsCollection.distinct("lastname")
db.friendsCollection.distinct("lastname",{"email":{$regex:".*friends"}})
db.friendsCollection.count()
// without condition, this will check the metadata and return the count.
// It will not be accurate all the time due to multi-node architecture
// and the time taken to sync.
db.friendsCollection.count({"lastname":"Geller"})
// newer syntaxes always query need to be passed, it’s slower
db.friendsCollection.countDocuments({"lastname":"Geller"})
// newer syntaxes to get an overall idea using metadata, quick result
db.friendsCollection.estimatedDocumentCount({"lastname":"Geller"})