A pipeline stage specification object must contain exactly one field

why i m getting thois error.why aggregation is faild here …

db.wallpost.aggregate([{
    $match: {
		$and: [
			{ $or: [
				{ postType: 1 },
				{ postType: 2 },
				{ postType: 3 },
				{ from: ObjectId("61e96b26a88909603ea95ffc") },
				{ timeZone:"Asia/Kolkata" }
			]},
			{ from: { '$in': ["5ff58fa655bd3c5769319c08"] } },
			{ postType: { $ne: 3 } },
			{ isDelete: 0 },
		]},
	'$lookup': {
		from: 'blockwallpost',
		localField: '_id',
		foreignField: 'wallpost',
		as: 'blockWallUser'
	},
	$addFields: {
		"feed_Block_filter": {
			"$filter":{
				input:"$blockWallUser",
				"as": "feed_Blocked",
				"cond": {
					"$eq": ["$$feed_Blocked.user", "$userId"],
					cond: {
						"$eq": ["$$feed_Blocked.wallpost","_id"]
					},
				}
			}
		}
    }},
    { $project: {"_id_id":0,"place":1} }
]).toArray

uncaught exception: Error: command failed: {
        "ok" : 0,
        "errmsg" : "A pipeline stage specification object must contain exactly one field.",
        "code" : 40323,
        "codeName" : "Location40323"
} : aggregate failed 

Hi @priyanka_shrivastva,

Welcome to the community!

“errmsg” : “A pipeline stage specification object must contain exactly one field.”

Each pipeline stage should be enclosed in its own curly brackets as a document. With regards to the command you’ve provided, the first document contains $match, $lookup and $addFields in a single document. However, each of these pipeline stages need to instead be in a separate document of their own.

I haven’t ran this aggregation pipeline on my own test system but I believe it should be like the example below:

db.wallpost.aggregate([
{'$match': {
		$and: [
			{ $or: [
				{ postType: 1 },
				{ postType: 2 },
				{ postType: 3 },
				{ from: ObjectId("61e96b26a88909603ea95ffc") },
				{ timeZone:"Asia/Kolkata" }
			]},
			{ from: { '$in': ["5ff58fa655bd3c5769319c08"] } },
			{ postType: { $ne: 3 } },
			{ isDelete: 0 },
		]}
	},
{'$lookup': {
		from: 'blockwallpost',
		localField: '_id',
		foreignField: 'wallpost',
		as: 'blockWallUser'
	}
},
{'$addFields': {
		"feed_Block_filter": {
			"$filter":{
				input:"$blockWallUser",
				"as": "feed_Blocked",
				"cond": {
					"$eq": ["$$feed_Blocked.user", "$userId"],
					cond: {
						"$eq": ["$$feed_Blocked.wallpost","_id"]
					}
				}
			}
		}
    }
},
{'$project': 
	{"_id_id":0,"place":1} 
}
]).toArray()

If you have further questions regarding the aggregation, please provide example documents and the desired output.

In addition to the above, you may find the aggregation documentation useful.

Regards,
Jason

1 Like