Skip to content

Instantly share code, notes, and snippets.

@sepehr
Last active November 18, 2015 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sepehr/0e4608c6ed0f02523288 to your computer and use it in GitHub Desktop.
Save sepehr/0e4608c6ed0f02523288 to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment