sepehr
12/20/2014 - 1:08 PM

MongoDB: Remove an array from an embedded document

MongoDB: Remove an array from an embedded document

//
// Find documents with sub-document query:
//
db.collection_name.find({
	relays: {
		$elemMatch: {
			timestamp: {$gte: 1447628400},
			status:    "RELAY_STATUS_ERROR_SUBMIT",
			app_id:    ObjectId("537cb1b5fc20a0eb47000000")
		}
	}
}).pretty();

//
// Remove subdocument from an embedded array:
//
db.collection_name.update(
// Matches parent document:
{
	relays: {
		$elemMatch: {
			timestamp: {$gte: 1447628400},
			status:    "RELAY_STATUS_ERROR_SUBMIT",
			app_id:    ObjectId("537cb1b5fc20a0eb47000000")
		}
	}
}, 

// Removes sub-document from the embedded array:
{
	$pull: {
		relays: {
		  timestamp: {$gte: 1447628400},
		  status:    "RELAY_STATUS_ERROR_SUBMIT",
		  app_id:    ObjectId("537cb1b5fc20a0eb47000000")
		}
	}
}, false, true);