Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:20:23+00:00 2026-06-13T11:20:23+00:00

I am starting out with mongodb and having hard time trying to query nested

  • 0

I am starting out with mongodb and having hard time trying to query nested documents. I have two schemas:

var LinkSchema = new mongoose.Schema({
    url: String,
    name: String
});

var UserSchema = new mongoose.Schema({
    name: String,
    links: [LinkSchema]
});

As you can see, I am just tying to build a simple bookmarking tool. Each user has a name and a collection of links. Each link has a name and a url.

Now, what I am trying to do is for example, see if a link already exists in someone’s links array. I would like to be able to do something like this (Trying to get vlad’s link collection and then see if the query link already belongs to the collection or not):

app.get("/:query", function(req, res){
  User.findOne({"name":"vlad"}, function(err, user){
    user.links.find({"url":req.params.query}, function(err, foundLinks){
      if(foundLinks){
        res.send("link already exists!");
      } else {
        res.send("link doesn't exist!");
      }
    });
  });
});

Of course, this code doesn’t work, because apparently I can’t do a “user.links.find()”. I guess I can just do a user.links.map to extract only urls and then run a membership query against it. But I think this would be far from the right solution. There’s gotta be a way to do something like this natively using DB queries. Can someone help? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:20:24+00:00Added an answer on June 13, 2026 at 11:20 am

    You can query an embedded document in mongoose like this

       User.find({'links.url':req.params.query}, function(err, foundUsers){
          // ---
       });
    

    and to find the links that belong to the user “vlad”, you can write

       User.find({name:'vlad','links.url':req.params.query}, function(err, foundUsers){
          // ---
       });
    

    This will do the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting out with MongoDB and JDO (DataNucleus.) I'm trying to figure out
Just starting out with subversion, have set up repos for 3 current projects and
So I am starting out on company project that will have several components: At
I'm starting out with backbone and I'm trying to create a simple view that
Starting out with Symfony2 + Doctrine. I have a table with User objects (fos_user),
Starting out with php, I have written a basic authentication script which prints out
I starting out in assembla and have a code from my senior that I
Just starting out in using Test::Unit but having a problem nailing down a redirect
I am trying to figure out MongoDB in order to evaluate whether it is
Just starting out to program in python and I'm having the following issue. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.