2

I'm new to MongoDB shell and I need some help !

I have 2 collections: movies and comments. I want to store a value from the second collection and use it to find something in the first.

This is what I have tried :

var result = db.comments.findOne({name: "Arya Stark"})
var movieId = JSON.stringify(result.movie_id.valueOf())
db.movies.findOne({_id: ObjectId($$movieId)})

The above prints that $$movieId is not defined. I have also tried :

db.movies.find({_id: {$toObjectId: movieId}})

and even though my version is 4.2.3, it prints:

unknown operator: $toObjectId.

I have tried everything, I don't know what else to do.

EDIT :

Here's the printed result :

{
        "_id" : ObjectId("5a9427648b0beebeb6957d48"),
        "name" : "Arya Stark",
        "email" : "[email protected]",
        "movie_id" : ObjectId("573a1392f29313caabcd9906"),
        "text" : "Laborum incidunt asperiores accusamus facilis vitae ut quidem. Praesentium provident explicabo odit dolores unde amet architecto. Iure id vero temporibus assumenda eum quia.",
        "date" : ISODate("1978-07-29T23:20:45Z")
}

Basically I want to take the movie_id from the collection comments and use it in the collection movies to search from which movie it is.

2
  • Well... I afraid we should have before hand a sample of both collections Commented Feb 19, 2020 at 22:21
  • Edited! Hope it's helpful! Commented Feb 19, 2020 at 23:03

1 Answer 1

1

You don't need to do two database calls, instead use $lookup for JOINS, try to execute below query in shell :

db.comments.aggregate([{ $match: { name: "Arya Stark" } }, {
    $lookup:
    {
        from: "movies",
        localField: "movie_id",
        foreignField: "_id",
        as: "movie"
    }
}, { $unwind: '$movie' }])

Your two issues are :

  1. when you use $$ in your query that respective variable has to be declared locally in query itself.
  2. $toObjectId is an aggregation operator, which can't be used like that in .find().
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your response! However this is not what I'm trying to do. To be more specific I want to find one random comment from user "Arya Stark", store the movie_id and then use it to find the specific movie from the "movies" collection. The thing is that the _id field accepts values of ObjectId type and I only have the id in string. For example what I have is: "573a1392f29313caabcd9906" but I want it to look like this ObjectId("573a1392f29313caabcd9906"). I can't find a way to pass my variable into the query..
@MaryVoulieri : So you mean to say you need random doc from comments collection with specific user ? So for that case add this stage{ $sample: { size: 1 } } in between $match & $lookup. which would get random document for filter { name: "Arya Stark" } & using this query you can do things in one DB call & should work, let me know if doesn't work for you - will update answer..
Thank you very much that's exactly what I wanted to do!
What can be used instead of $toObjectId in case of find()?
@SamuraiJack : what are you trying to do ? Db version ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.