insert record

db.pub.insert( { "code": 1, "title": "title1", "author": "author1", "publisher": "publisher1", "pub_code": 1001 } )
{
    "_id" : ObjectId("56bf243a2b2b00a1633a9019"),
    "code" : 1.0000000000000000,
    "title" : "title1",
    "author" : "author1",
    "publisher" : "publisher1",
    "pub_code" : 1001.0000000000000000
}

insert array

db.pub.update( { "pub_code":1001 }, { $push : { "distributor": { "name":"distributor1", "pub_code":2001,"sell_rate":60 } } })
{
    "_id" : ObjectId("56bf243a2b2b00a1633a9019"),
    "code" : 1.0000000000000000,
    "title" : "title1",
    "author" : "author1",
    "publisher" : "publisher1",
    "pub_code" : 1001.0000000000000000,
    "distributor" : [ 
        {
            "name" : "distributor1",
            "pub_code" : 2001.0000000000000000,
            "sell_rate" : 60.0000000000000000
        }
    ]
}

insert subarray

db.pub.update( { "pub_code":1001 }, { $push : { "distributor": { "name":"distributor2", "pub_code":2002,"sell_rate":61 } } })
{
    "_id" : ObjectId("56bf243a2b2b00a1633a9019"),
    "code" : 1.0000000000000000,
    "title" : "title1",
    "author" : "author1",
    "publisher" : "publisher1",
    "pub_code" : 1001.0000000000000000,
    "distributor" : [ 
        {
            "name" : "distributor1",
            "pub_code" : 2001.0000000000000000,
            "sell_rate" : 60.0000000000000000
        }, 
        {
            "name" : "distributor2",
            "pub_code" : 2002.0000000000000000,
            "sell_rate" : 61.0000000000000000
        }
    ]
}

update subarray’s field

db.pub.update({pub_code:1001,"distributor.pub_code":2001}, { $set: {"distributor.$.pub_code":20001,"distributor.$.sell_rate":61.5}})
{
    "_id" : ObjectId("56bf243a2b2b00a1633a9019"),
    "code" : 1.0000000000000000,
    "title" : "title1",
    "author" : "author1",
    "publisher" : "publisher1",
    "pub_code" : 1001.0000000000000000,
    "distributor" : [ 
        {
            "name" : "distributor1",
            "pub_code" : 20001.0000000000000000,
            "sell_rate" : 61.5000000000000000
        }, 
        {
            "name" : "distributor2",
            "pub_code" : 2002.0000000000000000,
            "sell_rate" : 61.0000000000000000
        }
    ]
}

update subarray’s array

db.pub.update({pub_code:1001,"distributor.pub_code":2002}, { $set: {"distributor.$": {"name":"distributor22","pub_code":20002,"rate":62.5}}})
{
    "_id" : ObjectId("56bf243a2b2b00a1633a9019"),
    "code" : 1.0000000000000000,
    "title" : "title1",
    "author" : "author1",
    "publisher" : "publisher1",
    "pub_code" : 1001.0000000000000000,
    "distributor" : [ 
        {
            "name" : "distributor1",
            "pub_code" : 20001.0000000000000000,
            "sell_rate" : 61.5000000000000000
        }, 
        {
            "name" : "distributor22",
            "pub_code" : 20002.0000000000000000,
            "rate" : 62.5000000000000000
        }
    ]
}

Aggregate

db.test.aggregate([				
	{ $match: {} },			
	 { $unwind: "$order_list" },			
	{ $group: 			
		{ "_id":		
			{	
				order_no: "$order_no",
				order_date: "$order_date",
				fran_order_no: "$fran_order_no",
				fran_code: "$fran_code",
				fran_name: "$fran_name"
			},	
			sell_price: { $sum: "$order_list.sell_price" },	
			buy_price: { $sum: "$order_list.buy_price" }	
		}		
	},			
	{ $sort: { order_no: -1 } }			
])				

booklist가 없으면 집계 자체가 않됨

db.order.aggregate([				
	{ $match: {} },			
	{ $group: 			
		{ "_id":		
			{	
				"order_no": "$order_no",
				"remarks": "$remarks"
			},	
			"price": { $sum: "$booklist.price" },	
			"warehousing_count": { $sum: "$worklist.warehousing_count" }	
		}		
	},			
	{ $sort: { order_no: -1 } }			
	])

booklist가 없으면 record 검색이 않됨

db.order.aggregate([				
	{ $match: {} },			
	{ $unwind: "$booklist" },			
	{ $group:			
		{ "_id":		
			{	
				"order_no": "$order_no",
				"remarks": "$remarks"
			},	
			"price": { $sum: "$booklist.price" },	
			"warehousing_count": { $sum: "$worklist.warehousing_count" }	
		}		
	},			
	{ $sort: { order_no: -1 } }			
	])			

Aggregate

db.order.aggregate([				
	{ $match: {} },			
	{ $unwind: "$booklist" },			
	{ $group:			
		{ "_id":		
			{	
				"order_no": "$order_no"
			}	
			, "price" : { $sum: { $ifNull: [ "$booklist.price", 0 ] } }	
		}		
	},			
	{ $sort: { order_no: -1 } }			
	])

order 생성 시 booklist: [ { } ] 를 만들어 줘야 aggregate grouping됨

				
db.order.aggregate([				
	{ $match: {} },			
	{ $unwind: "$booklist" },			
	{ $group:			
		{ "_id":		
			{	
				$ifNull: [ "$booklist.price", "Unspecified" ]
			}	
		}		
	},			
	{ $sort: { order_no: -1 } }			
	])			

Aggregate

db.order.aggregate(				
	{ $match: { "status": "sendorder" } },			
	{ $unwind : "$worklist" },			
	{ $group : {			
			_id: { 	
				library_code: "$library.library_code", 
				library_name: "$library.library_name",
				sendorder_date: "$worklist.sendorder_datetime", 
				company_name:"$company.company_name", 
				company_code:"$company.company_code",
				publisher_code:
				list_price: "$worklist.list_price",
				sendorder_qty: "$worklist.sendorder_qty", 
				sell_price: "$worklist.sell_price", 
				delivery_qty : { $ifNull: [ "$worklist.delivery_qty", 0 ] },
				warehouseing_qty : { $ifNull: [ "$worklist.warehouseing_qty", 0 ] }
			}	
		}		
	},			
	{ $group : {			
			_id: {
				"sendorder_date": "$_id.sendorder_date",
				"publisher_code" : "$_id.publisher_code"
			},	
			list_price: { "$sum": "$_id.list_price" }, 	
			sendorder_qty: { "$sum": "$_id.sendorder_qty" }, 	
			sell_price: { "$sum": "$_id.sell_price" }, 	
			delivery_qty: { "$sum": "$_id.delivery_qty" }, 	
			warehouseing_qty: { "$sum": "$_id.warehouseing_qty" }, 	
		}		
	}			
)

Find if Exist

db.domain.find( {tag : {$exists:true}, $where:'this.tag.length>3'} )				
{				
	_id : 1001,			
	domainName : "google.com"			
	tag : [			
		search engine,		
		search,		
		find anything,		
		giant		
	]			
}