Skip to content

Instantly share code, notes, and snippets.

@fgm
Created August 24, 2022 17:25
Show Gist options
  • Save fgm/7e81af81c31f6c0aac4245ee29f4c3c3 to your computer and use it in GitHub Desktop.
Save fgm/7e81af81c31f6c0aac4245ee29f4c3c3 to your computer and use it in GitHub Desktop.
// Insert doc:
sections = [
{
slug:"introduction",
title:"Introduction",
videos:[
{"k": "v0"},
{"k": "v1"},
{"_id": ObjectId('abcdefghijkl')}
]
}
];
doc = {sections: sections};
// {
// sections: [
// {
// slug: 'introduction',
// title: 'Introduction',
// videos: [
// { k: 'v0' },
// { k: 'v1' },
// { _id: ObjectId("6162636465666768696a6b6c") }
// ]
// }
// ]
// };
db.linkedin.insertOne(doc);
// Delete nested object:
db.linkedin.updateOne(
{},
{
$pull: {
"sections.0.videos": {
_id: ObjectId("6162636465666768696a6b6c")
}
}
}
)
// {
// acknowledged: true,
// insertedId: null,
// matchedCount: 1,
// modifiedCount: 1,
// upsertedCount: 0
// }
// Verify:
db.linkedin.findOne({_id:ObjectId("6306599912d8dbd883cbb345")})
// {
// _id: ObjectId("6306599912d8dbd883cbb345"),
// sections: [
// {
// slug: 'introduction',
// title: 'Introduction',
// videos: [ { k: 'v0' }, { k: 'v1' } ]
// }
// ]
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment