0

I come from RDBMS background and finding it difficult to get myself aligned with mongodb and have been struggling to generate just the array of values. Once i get the array, I need to use these array values in $condition clause of aggregate function using $in

I have a collection which returns me :

db.Users.find({user_id : 1623}, { "Friends" : 1, _id : 0})

And content:

{ "Friends" : [ 708, 784, 1495, 212, 1918, 2007, 1439, 1634, 649 ] }

I would only require the array of values than document type:

[ 708, 784, 1495, 212, 1918, 2007, 1439, 1634, 649 ] 

Is there a easier way to achieve this.

2 Answers 2

3

Use .distinct() instead, as it basically just returns an array:

db.Users.distinct("Friends", { "user_id": 1623 })

But I really think you a being pedantic, and should just accept the result "as is" and use the array for the field instead.

Sign up to request clarification or add additional context in comments.

1 Comment

@Alwyn On this site "thanks" is called accepting the answer.. Helps others know that this is what works as a solution as well.
0

You can directly ask for Friends array. ie: Assuming user_id is unique:

var arr = db.Users.findOne({user_id : 1623}).Friends;

2 Comments

Even this works.. Bieng a rdbms guy i prefer distinct :-)
That is fine, I am an RDBMS guy too, but be aware they are not the same thing. In this case, distinct might be what you want though.

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.