Skip to content

Instantly share code, notes, and snippets.

@Humberd
Last active May 16, 2018 15:14
Show Gist options
  • Save Humberd/53b6d211419cd82914e4c4ecd3f3c485 to your computer and use it in GitHub Desktop.
Save Humberd/53b6d211419cd82914e4c4ecd3f3c485 to your computer and use it in GitHub Desktop.
////////////////////////////////// task1
db.Film.createIndex({address: "text"})
/////////////////////////////////// task2
db.zadanieMongo.find(
{
age: {$lt: 20},
$text: { $search: "California Texas Pennsylvania" }
},
{
_id: 0,
name: 1,
age: 1,
address: 1
}
)
///////////////////////////////// task3
db.Film.update({
$text: {
$search: "California"
}
},
{
$set: {
activity: [
{
data: new Date(),
hobby: ""
},
{
data: new Date(),
hobby: ""
},
{
data: new Date(),
hobby: ""
},
{
data: new Date(),
hobby: ""
}
]
}
},
{
multi: true
})
AFFECTED 5
////////////////////////////////// task4
db.Film.update({
$text: {
$search: "California"
}
},
{
$push: {
activity: {
$each: [
{
data: new Date(),
hobby: 'pilka nozna'
}
],
$sort: { data: -1 },
$slice: 4
}
}
},
{
multi: true
})
AFFECTED 5
/////////////////////// task5
db.Film.find({
activity: {
$exists: false
}
}).count()
RETURNS 238
/////////////////////// task6
db.zadanieMongo.aggregate(
{
$match:
{
favoriteFruit: {$exists: true}
}
},
{
$group:
{
_id: "$favoriteFruit",
suma: {$sum: 1}
}
}
)
///////////////////////// task7
db.zadanieMongo.aggregate(
{
$unwind: "$tags"
},
{
$group:
{
_id: "$tags",
count: {$sum: 1}
}
}
)
//////////////////////// task8
db.Film.update({
$text: {
$search: "Alaska"
}
},
{
$inc: {
age: 1
}
},
{
multi: true
})
AFFECTED 4
////////////////////////// task9
db.Film.find({
isActive: {
$exists: true
}
}).forEach(function (doc) {
var isActive = doc.isActive;
if (isActive) {
doc.isActive = "yes"
} else {
doc.isActive = "no"
}
db.Film.save(doc)
})
//////////////////////// task10
db.zadanieMongo.getIndexes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment