MongoDB aggregation pipeline not working properly - mongodb

I'am trying to make an exercise with MongoDB database where I need to find movies in a movies collection , where the same person appears in cast, directors, and writers.
I tried this code :
//first match the writers, directors and cast arrays which are not empty.
db.movies.aggregate( [ {$match : {$and : [ {writers: {$elemMatch: {$exists: true}}} , {directors: {$elemMatch: {$exists: true}}} , {cast: {$elemMatch : {$exists: true}}} ]}}
//then make an intersection between the matched results
, {$project: {"labers of love" :{$setIntersection: ["$writers", "$cast", "$directors"]}, _id: 0 } } ] ).pretty()
However , when I run this command I still get empty arrays which should not be the case. I'am not sure if this pipeline is working properly. When I run only the $match command I dont seem to get any empty array.

Related

MongoDB Aggregate a sub field of an array (Array is main field)

I have a database in MongoDB. There are three main fields: _id, Reviews, HotelInfo. Reviews and HotelInfo are arrays. Reviews has a field called Author. I would like to print out every author name (once) and the amount of times they appear within the dataset.
I tried:
db.reviews.aggregate( {$group : { _id : '$Reviews.Author', count : {$sum : 1}}} ).pretty()
A part of the results were:
"_id" : [
"VijGuy",
"Stephtastic",
"dakota431",
"luce_sociator",
"ArkansasMomOf3",
"ccslilqt6969",
"RJeanM",
"MissDusty",
"sammymd",
"A TripAdvisor Member",
"A TripAdvisor Member"
],
"count" : 1
How it should be:
{ "_id" : "VijGuy", "count" : 1 }, { "_id" : "Stephtastic", "count" : 1 }
I posted the JSON format below.
Any idea on how to do this would be appreciated
JSON Format
Lets assume that this is our collection.
[{
_id: 1,
Reviews: [{Author: 'elad' , txt: 'good'}, {Author: 'chen', txt: 'bad'}]
},
{
_id: 2,
Reviews: [{Author: 'elad', txt : 'nice'}]
}]
to get the data as you want we need to first use the unwind stage and then the group stage.
[{ $unwind: '$Reviews' }, {$group : { _id : '$Reviews.Author', count : {$sum : 1}}}]
You need to first unwind the collection by the Reviews field.
after the unwind stage our data in the pipeline will look like this.
{_id:1, Reviews: {Author: 'elad' , txt: 'good'}},
{_id:1, Reviews: {Author: 'chen' , txt: 'bad'}},
{_id:2, Revies: {Author: 'elad', txt : 'nice'}
The unwind created a document for each element in Reviews array with the element itself and his host document. Now its easy to group in useful ways as you want. Now we can use the same group that you wrote and we will get the results.
after the group our data will look like this:
[{_id: 'elad',sum:2},{_id: 'chen', sum: 1}]
Unwind is a very important pipeline stage in the aggregation framework. Its help us transform complex and nested documents into flat and simple, and that help us to query the data in different ways.
What's the $unwind operator in MongoDB?

How do I project an element of an array in mongo?

I have a mongo document that contains something like
{date: [2018, 3, 22]}
and when I try to project this into a flat JSON structure with these fields concatenated, I always get an array with 0 elements, eg. just extracting the year with
db.getCollection('blah').aggregate([
{$project: {year: "$date.0"}}
])
I get
{"year" : []}
even though matching on a similar expression works fine, eg.
db.getCollection('blah').aggregate([
{$match: {"$date.0": 2018}}
])
selects the documents I would expect just fine.
What am I doing wrong? I've searched mongo documentation and stackoverflow but could find nothing.
For $project you should use $arrayElemAt instead of dot notation which works only for queries.
db.getCollection('blah').aggregate([
{$project: {year: { $arrayElemAt: [ "$date", 0 ] }}}
])
More here

$split and return the first array element in mongodb query

[
{lecture : "427#Math"},
{lecture : "217#Science"},
{lecture : "7#History"},
{lecture : "12#Music"}
]
Assume I have the database structure above. I want to return only the lecture code.
What have I done so far?
db.collection.aggregate([
{$project: {"lecture": {$split: ["$lecture" , "#"]}}}
])
But this returns as collection ["427" , "Math"]. How can I return only the lecture code which is the part that comes before the # character.
You can use $arrayElemAt to return only first item from $split result:
db.collection.aggregate([
{$project: {"lecture": {$arrayElemAt:[{$split: ["$lecture" , "#"]}, 0]}}}
])

Mongodb: Combine $in and $nin

In my collection posts I've documents like this
[{
_id : ObjectId("post-object-id"),
title : 'Post #1',
category_id : ObjectId("category-object-id")
}]
I need to make some queries where I those a range of posts based on their category_id (can be multiple ids) but exclude some of them.
I've tried with the query (in shell):
db.posts.find({$and: [
{_id: { $nin : ['ObjectId("post-object-id")']}},
{category_id : { $in : ['ObjectId("category-object-id")']}}
]})
I returns 0 if count().
However, if I change the category_id attribute and remove the $in and just include one ID it work, like this:
db.posts.find({$and: [
{_id: { $nin : ['ObjectId("58a1af81613119002d42ef06")']}},
{category_id : ObjectId("58761634bfb31efd5ce6e88d")}
]})
but this solution only enables me to find by one category.
How would I got about combining $in and $nin with objectId's in the same manner as above?
This will work, just remove single quotes around ObjectId
db.posts.find({$and: [
{_id: { $nin : [ObjectId("post-object-id")]}},
{category_id : { $in : [ObjectId("category-object-id")]}}
]})
You should not put single quotes around ObjectId, it make them strings

Multiple Mongo Projections

I am running a query from my Meteor server, but for some reason only the first projection is catching.
Users.find({"services.facebook" : {$exists : true}}, {"_id": {$nin: doNotCount}}).fetch()
only returns facebook users (disregarding {"_id": {$nin: doNotCount}})
Users.find(, {"_id": {$nin: doNotCount}}, {"services.facebook" : {$exists : true}}).fetch()
only returns users not in a given array (disregarding {"services.facebook" : {$exists : true}})
from the documentation, it looks like this is possible:
https://docs.mongodb.org/manual/reference/operator/projection/positional/
but im not having any luck
The query is the first parameter only, the second parameter deals with sorting, limits, restricting the fields to be returned etc...
Change to:
Users.find({ "services.facebook" : {$exists : true}, "_id": {$nin: doNotCount }}).fetch()