Search This Blog

2023/04/16

MongoDb:$unset & $unwind

Here we look further into $unwind.In latter part we will look
into unset.

we will use collection "clothing".

First drop the collection "clothing" if pre-exist.

db.clothing.drop()

Now we will insert some document in "clothing" collection.

db.clothing.insertMany([

{
"_id": 1,
"item": "Shirt",
"sizes": [
"S",
"M",
"L"
]
},
{
"_id": 2,
"item": "Shorts",
"sizes": []
},
{
"_id": 3,
"item": "Hat",
"sizes": "M"
},
{
"_id": 4,
"item": "Gloves"
},
{
"_id": 5,
"item": "Scarf",
"sizes": null
},
{
"_id": 6,
"item": "Handkerchief",
"sizes": undefined
}
])

Here in case of our last entry for "Handkerchief" size is undefined,
yet when inserted it becomes null.

just run below query & confirm yourself.

db.clothing.find({});


Here "sizes" field is array in some document,null in some document,
in some document it is empty array.

db.clothing.aggregate( [
{
$unwind: { path: "$sizes"}
}
])
Output:
[
{ _id: 1, item: 'Shirt', sizes: 'S' },
{ _id: 1, item: 'Shirt', sizes: 'M' },
{ _id: 1, item: 'Shirt', sizes: 'L' },
{ _id: 3, item: 'Hat', sizes: 'M' }
]

The documents where size array is empty or null does not
appear in our output.

If you want the null,undefined or empty array [],should also appear
in result you need to modify the query.As follows

db.clothing.aggregate( [
{
$unwind: { path: "$sizes",preserveNullAndEmptyArrays: true}
}
])

Output:
[
{ _id: 1, item: 'Shirt', sizes: 'S' },
{ _id: 1, item: 'Shirt', sizes: 'M' },
{ _id: 1, item: 'Shirt', sizes: 'L' },
{ _id: 2, item: 'Shorts' },
{ _id: 3, item: 'Hat', sizes: 'M' },
{ _id: 4, item: 'Gloves' },
{ _id: 5, item: 'Scarf', sizes: null },
{ _id: 6, item: 'Handkerchief', sizes: null }
]

$unset:
The $unset operator deletes a particular field.

For collection below

db.books.insertMany([
{
"_id": 1,
title: "Antelope Antics",
isbn: "0001122223334",
author: { last: "An", first: "Auntie"},
copies: [
{ warehouse: "A", qty: 5 },
{ warehouse: "B", qty: 15}
]
},
{
"_id": 2,
title: "Bees Babble",
isbn: "999999999333",
author: { last: "Bumble", first: "Bee"},
copies: [
{ warehouse: "A", qty: 2},
{ warehouse: "B", qty: 5}
]
}
])

This query will give result by excluding "copies" field

db.books.aggregate([
{
$unset: "copies"
}
])
Output:
[
{
_id: 1,
title: 'Antelope Antics',
isbn: '0001122223334',
author: { last: 'An', first: 'Auntie' }
},
{
_id: 2,
title: 'Bees Babble',
isbn: '999999999333',
author: { last: 'Bumble', first: 'Bee' }
}
]
we can exclude muliple fields simultenously.Here is How

db.books.aggregate([
{ $unset: ["copies","author"]}
])

Output:
[
{ _id: 1, title: 'Antelope Antics', isbn: '0001122223334' },
{ _id: 2, title: 'Bees Babble', isbn: '999999999333' }
]

Even we can exclude certain fields from object & object array.
Here "author" is an object we are excluding "author.first".
For "copies" object array we afre excluding "warehouse"

db.books.aggregate([
{
$unset: [
"isbn",
"author.first",
"copies.warehouse"
]
}
])

Output:
[
{
_id: 1,
title: 'Antelope Antics',
author: { last: 'An' },
copies: [ { qty: 5 }, { qty: 15 } ]
},
{
_id: 2,
title: 'Bees Babble',
author: { last: 'Bumble' },
copies: [ { qty: 2 }, { qty: 5 } ]
}
]

No comments:

Post a Comment