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

  • Home
  • SEARCH
  • 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 7874611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:50:23+00:00 2026-06-03T02:50:23+00:00

I am trying to figure out this map/reduce system in mongoDB. I have the

  • 0

I am trying to figure out this map/reduce system in mongoDB. I have the following basic schema/layout in my collection.

{
    _id: 1,
    name: n1,
    website: w1,
    tags: [
        myTag1,
        myTag3
    ]
}

{
    _id: 2,
    name: n2,
    website: w2,
    tags: [
        myTag2,
        myTag3
    ]
}

{
    _id: 3,
    name: n3,
    website: w3,
    tags: [
        myTag2,
        myTag4
    ]
}

How can I retrieve an array of unique tags? I would like this to be returned to me for further use.

{
    tags: [
        myTag1,
        myTag2,
        myTag3,
        myTag4
    ]
}

By the way this is what I have come up with, but it just returns the _id and tags of each item instead of combining the tags into a single object.

var map = function() {emit( this._id,{tags: this.tags});};

var reduce = function(key, values) {
    var t = [];

    values.forEach(function(doc) {
        var tags = doc.tags;
        tags.forEach(function(tag) {
            if (!(tag in t)) {
                t.push(tag);
            }
        });
    });

    return {tags: t};
};

var op = db.businesses.mapReduce(map, reduce, {out: "mr_results"});

db[op.result].find();
  • 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-03T02:50:24+00:00Added an answer on June 3, 2026 at 2:50 am

    There’s no need to use map-reduce in your case. Just use the distinct function:

    db.businesses.distinct('tags')
    

    You can try it in the mongo shell:

    > use test
    switched to db test
    > db.businesses.insert({tags: ['tag1', 'tag2']})
    > db.businesses.insert({tags: ['tag3', 'tag4']})
    > db.businesses.find()
    { "_id" : ObjectId("4fa05b2b036495bf4ac9c0cc"), "tags" : [ "tag1", "tag2" ] }
    { "_id" : ObjectId("4fa05b33036495bf4ac9c0cd"), "tags" : [ "tag3", "tag4" ] }
    
    > db.businesses.distinct('tags')
    [ "tag1", "tag2", "tag3", "tag4" ]
    

    Also, you should keep in mind that map/reduce in MongoDB is not suitable for real-time querying.

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

Sidebar

Related Questions

I'm trying to figure out if this is possible.. I have an image map
Trying to figure out this pseudo code. The following is assumed.... I can only
trying to figure out why this is happening - I have an input text
I'm trying to figure out if this is possible. I have a class, BaseGameEntity,
I'm trying to figure out why this code won't compile. I have interface A
I'm trying to figure out how to draw a color map like this: http://www.ritsplasman.com/images/colormap.png
I am new to map / reduce and trying to figure out a way
I've been trying to figure out now if i have a map in my
I'm trying to figure out this weird issue I've been having, and the root
I am trying to figure out this error from previous 3 hours. By searching,

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.