I want to get distinct and selected column names in mongodb - mongodb

I want to get distinct of coulmn1 and the output should be selected column names(column1, column5) in mongodb

Related

I want to create a index in each document in mongo which having a unique number like roll number

Currently i have a 3.1k documents each one have mobile and email field but i want to add a another filed with a unique number in all documents.
is there any query that can add a new field in each documents and insert number from 1 to end by incrementing?

Query jsonb column in Postgres against multiple values

I have a column 'carInfo' of type jsonbin my PostgreSQL database and I want to be able to query against multiple values. I want the query to return only those rows that match all criteria. For example, if I had multiple columns, I would write the query like this:
select *
from car
where name = 'BMW'
AND 'year' = 2020
Since my column is of type jsonb I can make use of the containment operator (#>) like this:
select *
from car
where carInfo #> cast('{"name":"BMW"}') as jsonb
AND carInfo #> cast('{"year":"2020"}')
but I want the query to be dynamic, so that the user can query by any attributes they want.
So, they will send a list of search terms (in this case, the elements of that list would be {"name":"BMW"} and {"year":"2020"}.
Assuming, that I have the list as above, how would the query look like if I wanted to achieve the same result as when using the AND operator?
I tried it like this:
select *
from car
where carInfo #> any(array['{"name":"BMW"}', '{"year":"2020"}']::jsonb[])
but it acts the same way as when using the OR operator. I need to find those rows that contain BOTH the search terms
You could use #> ALL, but this should be better:
WHERE carInfo #> cast('{"name":"BMW", "year": "2020"}' as jsonb)

How to remove null matching document rows in case of populate in mongodb

Hello all I am new to mongodb and nodejs application .i want to join two documents and filter the same by query but it return null when no matching is found in second document i do not want null i want to remove that complete row

How to select a single column and group array

I want to select a single column in mgo which is array of strings currently using this returns a empty array
classifiedColl.Find(nil).Distinct("Tags", &tags)
and i want to group it how do i do it. The value i want to group is array of strings.

How to retrieve all values from a column in mongoDB

I am wondering how to retrieve all values stored in a column in mongoDB, and put them in a list. find() only get all the fields, and specify <field>: <value> isn't an option as well.
If you are expecting to retrieve unique values in a column then distinct should work for you. Following is the syntax :
db.yourcollection.distinct("yourfield");
Learn more about distinct here