With the following data
// 1
{
"_id": ObjectId("64a610b3dec8ecf2749147f9"),
"key": "mini/ttttkey",
"value": {
"posts": [
{
"id": NumberInt("1"),
"title": "json-server",
"author": "typicode"
},
{
"id": NumberInt("2"),
"title": "json-server111",
"author": "typicode222"
}
],
"comments": [
{
"id": NumberInt("1"),
"body": "some comment",
"postId": NumberInt("1")
},
{
"id": NumberInt("1"),
"body": "111some comment",
"postId": NumberInt("1")
}
],
"profile": {
"name": "typicode"
}
}
}
// 2
{
"_id": ObjectId("64a611c5177d004d13de7b58"),
"key": "mini/key1",
"value": {
"posts": [
{
"id": NumberInt("1"),
"title": "json-server",
"author": "typicode"
},
{
"id": NumberInt("2"),
"title": "json-server111",
"author": "typicode222"
},
{
"id": NumberInt("2"),
"title": "json-server111",
"author": "typicode222"
}
],
"comments": [
{
"body": "some comment",
"postId": NumberInt("1")
},
{
"body": "111some comment",
"postId": NumberInt("2")
}
],
"profile": {
"name": "typicode",
"version": "12"
}
}
}
// 3
{
"_id": ObjectId("64a655b4d95c4856e539c9af"),
"key": "mini/key2",
"value": {
"posts": [
{
"id": NumberInt("1"),
"title": "json-server",
"author": "typicode"
},
{
"id": NumberInt("2"),
"title": "json-server111",
"author": "typicode222"
},
{
"id": NumberInt("2"),
"title": "json-server111",
"author": "typicode222"
}
],
"comments": [
{
"body": "some comment",
"postId": NumberInt("1")
},
{
"body": "111some comment",
"postId": NumberInt("2")
}
],
"profile": {
"name": "typicode",
"version": "12"
}
}
}
I would like to complete the following query:
Q1: get the filtered array data, like posts field must be json-server
{
"_id": ObjectId("64a610b3dec8ecf2749147f9"),
"key": "mini/ttttkey",
"value": {
"posts": [
{
"id": NumberInt("1"),
"title": "json-server",
"author": "typicode"
}
]
}
}
Q2: get the limited array data, like just get posts.slice(1,2)
// 1
{
"_id": ObjectId("64a610b3dec8ecf2749147f9"),
"key": "mini/ttttkey",
"value": {
"posts": [
{
"id": NumberInt("2"),
"title": "json-server111",
"author": "typicode222"
}
]
}
}
>Solution :
To achieve the desired results, you can use the aggregation framework in MongoDB to filter and manipulate the array data. Here are the queries to accomplish your requirements:
Q1: Get the filtered array data where the "posts" field must contain "json-server":
db.collection.aggregate([
{
$match: {
"value.posts.title": "json-server"
}
},
{
$project: {
_id: 1,
key: 1,
value: {
posts: {
$filter: {
input: "$value.posts",
as: "post",
cond: {
$eq: ["$$post.title", "json-server"]
}
}
}
}
}
}
]);
This query uses the $match stage to filter documents where the "posts" field contains the desired title ("json-server"). Then, the $project stage is used to reshape the output and only include the matching posts.
Q2: Get the limited array data where only the second element of the "posts" array is returned:
db.collection.aggregate([
{
$project: {
_id: 1,
key: 1,
value: {
posts: {
$slice: ["$value.posts", 1, 1]
}
}
}
}
]);
This query uses the $project stage and the $slice operator to extract a portion of the "posts" array. In this case, it retrieves the second element only.
Please note that you need to replace collection with the actual name of your MongoDB collection where the data is stored. Also, make sure you are running these queries in a MongoDB shell or using a compatible MongoDB driver.
The results of these queries will provide the desired filtered and limited array data based on the specified conditions.