Clean docs in collection by comparing them with reference - mongodb

I have a lot of complex JSON objects which are placed in the collection. For example:
{
"name": "Mike",
"price": "444",
"distance": 881,
"someFiend": 123,
"lots": [
{
"aa": "111",
"bb": "222"
},
{
"xx": "000"
}
],
"apps": [
{
"app": 1
},
{
"app": 2
}
]
}
I only want to project only those fields which are present in the following reference document:
{
"name": "",
"price": "",
"lots": [
{
"aa": "",
"bb": ""
}
]
}
Expected output:
{
"name": "Mike",
"price": "444",
"lots": [
{
"aa": "111",
"bb": "222"
}
]
}
Is there any way to iterate all documents in the collection and then filter out fields that are not present in the reference doc?

Related

How to recursively query the nested documents of mogodb?

{
"id": "1505036191456227329",
"materialList": [
{
"id": "1505035441229459457",
"model": "",
"parentId": "0",
},
{
"id": "1505035441229459458",
"model": "",
"parentId": "1505035441229459457",
}, {
"id": "1505035441229459459",
"model": "",
"parentId": "1505035441229459457",
},{
"id": "1505035441229459460",
"model": "",
"parentId": "1505035441229459459",
}
]
}
this is my data,I want to find all sub data IDS with ID 1505035441229459457 under the materiallist。
The result of the query is 1505035441229459458,1505035441229459459,1505035441229459460
You will have to use $elemMatch for this.
Ex -> db.collection.find( { materialList: { $elemMatch: {id:
1505035441229459457 } } })

mongodb distinct query values

I have the following mongodb documents:
{
"_id": "",
"name": "example1",
"colors": [
{
"id": 1000000,
"properties": [
{
"id": "1000",
"name": "",
"value": "green"
},
{
"id": "2000",
"name": "",
"value": "circle"
}
]
} ]
}
{
"_id": "",
"name": "example2",
"colors": [
{
"id": 1000000,
"properties": [
{
"id": "1000",
"name": "",
"value": "red"
},
{
"id": "4000",
"name": "",
"value": "box"
}
]
} ]
}
I would like to get distinct queries on the value field in the array where id=1000
db.getCollection('product').distinct('colors.properties.value', {'colors.properties.id':{'$eq': 1000}})
but it returns all values in the array.
The expected Result would be:
["green", "red"]
There are a lot of way to do.
$match eliminates unwanted data
$unwind de-structure the array
$addToSet in $group gives the distinct data
The mongo script :
db.collection.aggregate([
{
$match: {
"colors.properties.id": "1000"
}
},
{
"$unwind": "$colors"
},
{
"$unwind": "$colors.properties"
},
{
$match: {
"colors.properties.id": "1000"
}
},
{
$group: {
_id: null,
distinctData: {
$addToSet: "$colors.properties.value"
}
}
}
])
Working Mongo playground

check if a field of type array contains an array

Im using mongoose, I have the following data of user collection:
[{
"_id": "1",
"notes": [
{
"value": "A90",
"text": "math"
},
{
"value": "A80",
"text": "english"
},
{
"value": "A70",
"text": "art"
}
]
},
{
"_id": "2",
"notes": [
{
"value": "A90",
"text": "math"
},
{
"value": "A80",
"text": "english"
}
]
},
{
"_id": "3",
"notes": [
{
"value": "A80",
"text": "art"
}
]
}]
and I have as a parameters the following array: [ "A90", "A80" ]
so I want to make a query to use this array to return only the records that have all the array items in the notes (value) table.
So for the example above it will return:
[{
"_id": "1",
"notes": [
{
"value": "A90",
"text": "math"
},
{
"value": "A80",
"text": "english"
},
{
"value": "A70",
"text": "art"
}
]
},
{
"_id": "2",
"notes": [
{
"value": "A90",
"text": "math"
},
{
"value": "A80",
"text": "english"
}
]
}]
I tried the following find query:
{ "notes": { $elemMatch: { value: { $in: valuesArray } } }}
but it returns a record even if just one element in valuesArray exist.
it turned out to be quite easy:
find({ "notes.value": { $all: arrayValues } })

Mongodb: Apply group in a single collection with multiple relations

Example: A Collection name is Account which is having categories and upvotes and downvotes. Upvotes and downvotes are basically feeds of categories. The structure of the Collection is as follows:
{
"id": "585a2f2735cc577c178bda2b",
"category_ids": [
"5857e65c950cfd241818abc3",
"5857e92f950cfd241818abd0",
"5857e957950cfd241818abd2",
"5857f03f950cfd241818abd5"
],
"upVotes": [
{
"id": "585a6ccc055f93cbb10bd179",
"name": "career feed",
"category_ids": [
"5857e65c950cfd241818abc3"
]
},
{
"id": "5860bc714b7b3a400ef96d2a",
"name": "Treasurers and Controllers",
"category_ids": [
"5857e957950cfd241818abd2"
]
}
],
"downVotes": [
{
"id": "585a8fbb416ecf300c6ea969",
"name": "testinggggg",
"category_ids": [
"5857f03f950cfd241818abd5",
"5857f07c950cfd241818abd6"
]
},
{
"id": "585a8406354db7d811f9a405",
"name": "feeds5",
"category_ids": [
"5857e957950cfd241818abd2",
"5857f07c950cfd241818abd6"
]
}
]
}
I want to show downvotes and upvotes according to the category of user.
[Working on Mongo DB]
OUTPUT:
"Category": [{
"id": "585a6ccc055f93cbb10bd179",
"upvotes": [{
"id": "",
"name": ""
}, {
"id": "",
"name": ""
}],
"downvotes": [{
"id": "",
"name": ""
}, {
"id": "",
"name": ""
}]
}]

Order by nested document Mongodb

I'm having multiple documents in a collection, each document has this data structure :
{
"_id": {
"$id": "5429409c9ac25ebe338b4567"
},
"data": [
{
"data_id": "70",
"info_data": [
{
"data_id": "98",
"data_index": 0,
"value": "info data"
},
{
"data_id": "99",
"data_index": 0,
"value": "some info data
}
]
},
{
"data_id": "71",
"info_data": [
{
"data_id": "98",
"data_index": 0,
"value": "some data"
},
{
"data_id": "99",
"data_index": 0,
"value": "more data"
}
]
}
]
},
{
"_id": {
"$id": "542940ac9ac25ef6358b4567"
},
"data": [
{
....
I need to conditionally sort these documents. for example I need to sort all the data.info_data documents only when the data.info_data.data_id = 98 by data.info_data.value only
So basically I need to sort an inner document that only matches to some criteria (the inner document no the external one).
I guess I need to use aggregation with unwind but I'm not sure how.