Mongoose aggregation with discriminator - node.js

I use CosmosDB Mongo API with only one collection using discriminators.
I'm now facing a problem where I want to "join" other collections into one.
My common collection is named "alldata" and has discriminator keys (_type) "Session", "Room" and "User".
Session contains "roomId" and "speakerIds" (Array of user ids).
Session object:
{
"singleEntrance": false,
"accreditationRequired": false,
"closed": false,
"usersAtended": [],
"speakerIds": [
"5b573723930d9751768f266e",
"5b586b6032063a70632721d8"
],
"archived": false,
"_type": "Session",
"_id": "5b586f81ae744a7266524f84",
"title": "Bla bla",
"about": "",
"type": "SIS1",
"track": "Special Interest Session",
"venue": "Bla 1",
"roomId": "5b5731e764f0de4f9e4082ac",
"from": "2018-10-22T10:00:00.015Z",
"to": "2018-10-22T12:00:00.015Z"
}
Result I want:
{
"singleEntrance": false,
"accreditationRequired": false,
"closed": false,
"usersAtended": [],
"spikeri": [
{* LIST OF USER OBJECTS *}
],
"archived": false,
"_type": "Session",
"_id": "5b586f81ae744a7266524f84",
"title": "Bla bla",
"about": "",
"type": "SIS1",
"track": "Special Interest Session",
"venue": "Bla 1",
"room": {
"_id": "5b5731e764f0de4f9e4082ac",
"archived": false,
"_type": "Room",
"name": "Bla 1",
"location": "Hotel Bla",
"createdAt": "2018-07-24T14:04:23.370Z",
"updatedAt": "2018-07-24T14:04:23.370Z",
"__v": 0
},
"from": "2018-10-22T10:00:00.015Z",
"to": "2018-10-22T12:00:00.015Z"
}
Room works fine, but I just cannot get User ("speakers") to work.
Finally, here's the code:
async getAgenda() {
return await Session.aggregate([
{
// this query returns all Sessions...
$match: {
$and: [
{ from: { $gte: new Date("2018-10-22T10:00:00.015Z") } },
{ to: { $lte: new Date("2018-10-22 17:00:00.015Z") } },
]
}
},
{
$lookup: { from: "alldata", localField: "roomId", foreignField: "_id", as: "room" }
},
{
$unwind: { path: '$room' }
},
{
$lookup: { from: "alldata", localField: "speakerIds", foreignField: "_id", as: "spikeri" }
}
])
}
Update: User object (speaker == User):
{
"_id": "5b2ba7784ad0f226d8ae2788",
"group": "USER",
"verified": true,
"eventsRegistered": [
"5b2b77b74ad0f226d8ae2780",
"5b27ca01cc7eff056826acc7"
],
"eventsAttended": [],
"tickets": [
"5b27cc43cc7eff056826accb",
"5b27d284cc7eff056826acd1",
"5b27cceecc7eff056826acd0"
],
"contacts": [],
"meetings": [],
"_type": "User",
"email": "bla#blabla.us",
"password": "...",
"countryCode": "HR",
"firstName": "Blaman",
"lastName": "Bla bla",
"role": "SPEAKER",
"createdAt": "2018-06-21T13:26:16.686Z",
"updatedAt": "2018-07-25T14:21:42.055Z",
"__v": 0
"confirmationCode": "5375159",
"transactionIds": [
"..."
],
"transactionId": "...",
"gender": "",
"jobTitle": "",
"companyAddress": "",
"companyName": "",
"postalCode": "",
"city": "",
"country": "Croatia"
}
Thank you for help in advance!

Related

How to filter results from collection the $lookup in mongoose

i want to filter the result as the following in mongodb. I use $lookup to populate the result from another collection. Please check my following code
This code below is what i get
{
"_id": "5f3d563122de0730d0f6a754",
"barcode": "1234",
"productname": "Lays Packet",
"brandName": "Lays",
"productSize": "12",
"price": "12",
"quant": "12",
"imageurl": "http://localhost:3000/images/imageurl-1597855281050.jpg",
"remaining": "12",
"creator": "3d943b957fb5db510d824c5cbd6e8f7d",
"__v": 0,
"source": [
{
"_id": "5f3a9bbc325a074240a1a815",
"firstname": "test",
"lastname": "test",
"storename": "test",
"gst": "test",
"phoneNumber": 1,
"email": "1#demo.com",
"address1": "test",
"address2": "test",
"city": "test",
"state": "test",
"country": "test",
"zip": "1",
"password": "1",
"usertype": 3,
"unique_SHOP": "3d943b957fb5db510d824c5cbd6e8f7d",
"__v": 0
}
]
},
How to retrieve only unique_SHOP and zip from source listing.I want result like the one below with one or more fields
{
"_id": "5f3d563122de0730d0f6a754",
"barcode": "1234",
"productname": "Lays Packet",
"brandName": "Lays",
"productSize": "12",
"price": "12",
"quant": "12",
"imageurl": "http://localhost:3000/images/imageurl-1597855281050.jpg",
"remaining": "12",
"creator": "3d943b957fb5db510d824c5cbd6e8f7d",
"__v": 0,
"source": [
{
"zip": "1",
"unique_SHOP": "3d943b957fb5db510d824c5cbd6e8f7d",
}
]
},
The query i use
List.aggregate([
{$match:
{ productname: { $regex: req.params.query,$options: "i" }}
},
{ $lookup:{
from: "suppliers",
localField: "creator",
foreignField: "unique_SHOP",
as: "source"
}
},
])
You can try $lookup with pipeline,
$match condition of creator id
$project to display required fields
{
$lookup: {
from: "suppliers",
as: "source",
let: { creator: "$creator" },
pipeline: [
{
$match: {
$expr: { $eq: ["$$creator", "$_id"] }
}
},
{
$project: {
_id: 0,
zip: 1,
unique_SHOP: 1
}
}
]
}
}
Playground

Flter mongodb database using mongoose nodejs

I need to filter some users according to some fixed criteria. I have a user collection and a talent collection. The talent collection holds the reference to a master category collection.
What I need is to filter these users according to the category in the talent collection and some keys from the user collection.
For example I need to search for a user whose gender is 'male' and education 'BTech' and will have talents as a programmer and tester
my user collection is like,
{
"_id": "5f1939239bd35429ac9cd78f",
"isOtpVerified": "false",
"role": "user",
"adminApproved": 1,
"status": 0,
"languages": "Malayalam, Tamil, Telugu, Kannada",
"name": "Test user",
"email": "test#email.com",
"phone": "1234567890",
"otp": "480623",
"uid": 100015,
"bio": "Short description from user",
"dob": "1951-09-07T00:00:00.000Z",
"gender": "Male",
"education": "Btech",
"bodyType": "",
"complexion": "",
"height": "",
"weight": "",
"requests": [],
"location": {
"place": "place",
"state": "state",
"country": "country"
},
"image": {
"avatar": "5f1939239bd35429ac9cd78f_avatar.jpeg",
"fullsize": "5f1939239bd35429ac9cd78f_fullsize.png",
"head_shot": "5f1939239bd35429ac9cd78f_head_shot.jpeg",
"left_profile": "5f1939239bd35429ac9cd78f_left_profile.png",
"right_profile": "5f1939239bd35429ac9cd78f_right_profile.png"
},
"__v": 42,
"createdAt": "2020-07-23T07:15:47.387Z",
"updatedAt": "2020-08-18T18:54:22.272Z",
}
Talent collection
[
{
"_id": "5f38efef179aca47a0089667",
"userId": "5f1939239bd35429ac9cd78f",
"level": "5",
"chars": {
"type": "Fresher",
},
"category": "5f19357b50bcf9158c6be572",
"media": [],
"createdAt": "2020-08-16T08:35:59.692Z",
"updatedAt": "2020-08-16T08:35:59.692Z",
"__v": 0
},
{
"_id": "5f3b7e6f7e322948ace30a2c",
"userId": "5f1939239bd35429ac9cd78f",
"level": "3",
"chars": {
"type": "Fresher",
},
"category": "5f19359250bcf9158c6be573",
"media": [
{
"adminApproved": 0,
"status": 0,
"_id": "5f3c22573065f84a48e04a14",
"file": "id=5f1939239bd35429ac9cd78f&dir=test&img=5f1939239bd35429ac9cd78f_image_undefined.jpeg",
"description": "test",
"fileType": "image",
"caption": "test file"
},
{
"adminApproved": 0,
"status": 0,
"_id": "5f3c2d7a8c7f8336b0bfced2",
"file": "id=5f1939239bd35429ac9cd78f&dir=test&img=5f1939239bd35429ac9cd78f_image_1.jpeg",
"description": "this is a demo poster for testing",
"fileType": "image",
"caption": "A Test Poster"
}
],
"createdAt": "2020-08-18T07:08:31.532Z",
"updatedAt": "2020-08-18T19:35:22.899Z",
"__v": 2
}
]
And the category in the above document is a separate one populated to this. the category collection as,
[
{
"_id": "5f19359250bcf9158c6be573",
"status": true,
"title": "Testing",
"description": "Application tester",
"code": "test",
"characteristics": [],
"createdAt": "2020-07-23T07:00:34.221Z",
"updatedAt": "2020-07-23T07:00:34.221Z",
"__v": 0
},
{
"status": true,
"_id": "5f29829a705b4e648c28bc88",
"title": "Designer",
"description": "UI UX Designer",
"code": "uiux",
"createdAt": "2020-08-04T15:45:30.125Z",
"updatedAt": "2020-08-04T15:45:30.125Z",
"__v": 0
},
{
"_id": "5f19357b50bcf9158c6be572",
"status": true,
"title": "programming",
"description": "Java programmer",
"code": "program",
"createdAt": "2020-07-23T07:00:11.137Z",
"updatedAt": "2020-07-23T07:00:11.137Z",
"__v": 0
}
]
So my filter terms will be;
{
categories: ["5f19359250bcf9158c6be573", "5f19357b50bcf9158c6be572"],
minAge: 18,
maxAge: 25,
minHeight: 5,
maxHeight: 6,
minWeight: 50,
maxWeight: 80,
complexion: "white",
gender: "male",
}
And the expected result will be a user have both the above talents and followed conditions,
{
users: { ..User details.. },
medias: { ...medias from the matching talents.. }
}
If there are two collections you need to join them either by primary key or _id with foriegn fields and you can use $lookup with $match to filter down.
Documentation
You need to use $lookup with pipeline,
$match you condition for category match
$lookup to join users collection
$match conditions for users collections fields
$match exclude documents that don't found matching users of criteria passed in conditions
db.talents.aggregate([
{
$match: {
category: { $in: ["5f19359250bcf9158c6be573", "5f19357b50bcf9158c6be572"] }
}
},
{
$lookup: {
from: "users",
as: "users",
let: { userId: "$userId" },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$$userId", "$_id"] },
{ $eq: ["$gender", "Male"] },
{ $eq: ["$education", "Btech"] }
// ... add you other match criteria here
]
}
}
}
]
}
},
{ $match: { users: { $ne: [] } } }
])
Playground

How to change this response to simple array?

{
"success": true,
"message": "Result",
"data": [
{
"Here": [
{
"_id": "5ee97ee7f25d1c1482717bdf",
"email": "test1#test.io",
"profileImages": [],
"username": "test1",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location",
"firstName": "test1",
"lastName": "test1",
}
]
},
{
"Here": [
{
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2#test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]
}
],
}
What I am expecting is this
{
"success": true,
"message": "Result",
data: [
{
"_id": "5ee97ee7f25d1c1482717bdf",
"email": "test1#test.io",
"profileImages": [],
"username": "test1",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location",
"firstName": "test1",
"lastName": "test1"},
{
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2#test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]
}
Query I am using is for this response is below using aggregation in mongodb, lookup and project which is leading me to the some undesired response
db.collections.aggregate( [
{
$lookup: {
from: 'users',
as: 'Here',
let: {
whoDid: '$whoDid'
},
pipeline: [
{
"$match": { "$expr": { "$eq": ["$_id", "$$whoDid"] } }
},
{
$project: {
_id: 1,
email: 1,
profileImages: 1,
username: 1,
birthday: 1,
phoneNumber: 1,
firstName: 1,
lastName: 1,
fullName: 1,
// age: {$year: "$birthday"}
age: {
$divide: [{ $subtract: [new Date(), "$birthday"] },
(31558464000)]
}
}
}
],
}
},
{
$project:{
Here:1,
_id:0
}
} ,
])
who did table is one of the collection I have where I have stored the user Id and later I am populating the data using lookup
{
"_id" : ObjectId("5ee988eb1aac0022e15dbb7b"),
"whoDid" : ObjectId("5ee97ef2f25d1c1482717be1"),
"toWhomDid" : ObjectId("5ee97ec0f25d1c1482717bdd"),
"modified_at" : ISODate("2020-06-17T03:07:23.217Z"),
"created_at" : ISODate("2020-06-17T03:07:23.217Z"),
"__v" : 0
}
{
"_id" : ObjectId("5ee988eb1aac0022e15dbb7c"),
"whoDid" : ObjectId("5ee97ec0f25d1c1482717bdd"),
"toWhomDid" : ObjectId("5ee97ef2f25d1c1482717be1"),
"modified_at" : ISODate("2020-06-17T03:07:23.220Z"),
"created_at" : ISODate("2020-06-17T03:07:23.220Z"),
"__v" : 0
}
Can anyone suggest me any better option so that I can get a desired respose?
It is possible to use reduce method:
obj.data = obj.data.reduce((a, c) => {
a.push(...c.Here);
return a;
}, [])
An example:
let obj = {
"success": true,
"message": "Result",
"data": [ {
"Here": [ {
"_id": "5ee97ee7f25d1c1482717bdf", "email": "test1#test.io",
"profileImages": [], "username": "test1",
"birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location",
"firstName": "test1", "lastName": "test1",
}
]
},
{
"Here": [ {
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2#test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]
}
]
};
obj.data = obj.data.reduce((a, c) => {
a.push(...c.Here);
return a;
}, [])
console.log(obj);
Add these extra steps into your aggregation pipeline:
{
$unwind: "$Here"
},
{
$replaceWith: "$Here"
}
MongoPlayground
Note: You can replace $project: { _id: 1, email: 1, ... to this:
{
$addFields:{
age: {
$divide: [{ $subtract: [new Date(), "$birthday"] },(31558464000)]
}
}
}

Populate data from another collection mongoose

I have 3 collections users, profiles and trustedcontacts. Profiles and trustedcontacts have ref to users
My db collections
I have collection users
{
"_id": {
"$oid": "5c5ecaf6134fc342d4b1a9d5"
},
"name": "User",
"email": "user#gmail.com",
"password": "$2a$10$BXxwpMTFK1a0aWclaqJYve4f3SZyi/emwHKv5rY2GNzrPSEsIJhzi",
},
{
"_id": {
"$oid": "5c64968cae53a8202c963223"
},
"name": "User1",
"email": "user1#gmail.com",
"password": "$2a$10$BXxwpMTFK1a0aWclaqJYve4f3SZyi/emwHKv5rY2GNzrPSEsIJhzi",
},
{
"_id": {
"$oid": "5c69968cae53a8202c963554"
},
"name": "User1",
"email": "user1#gmail.com",
"password": "$2a$10$BXxwpMTFK1a0aWclaqJYve4f3SZyi/emwHKv5rY2GNzrPSEsIJhzi",
}
collection profiles
{
"_id": {
"$oid": "5c5ecb17134fc342d4b1a9d6"
},
"user": {
"$oid": "5c5ecaf6134fc342d4b1a9d5"
},
"handle": "handle",
"company": "test"
},
{
"_id": {
"$oid": "5c6496ebae53a8202c963224"
},
"user": {
"$oid": "5c64968cae53a8202c963223"
},
"handle": "handle1",
"company": ""
},
{
"_id": {
"$oid": "5c6496ebae53a8202c963224"
},
"user": {
"$oid": "5c69968cae53a8202c963554"
},
"handle": "handle2",
"company": ""
}
collection trustedcontacts
{
"_id": {
"$oid": "5d76008e4b98e63e58cb34cc"
},
"approvedTrustedContacts": [
{
"_id": {
"$oid": "5d764e411b7476462cf6b540"
},
"user": {
"$oid": "5c5ecaf6134fc342d4b1a9d5"
}
},
{
"_id": {
"$oid": "5d764e411b7476462cf6b541"
},
"user": {
"$oid": "5c64968cae53a8202c963223"
}
}
],
"pendingApprovalContacts": [],
"waitingForApprovalContacts": [],
"user": {
"$oid": "5d76008e4b98e63e58cb34cb"
}
}
//My Schemas
const UserSchema = new mongoose.Schema({
name: {
type: String,
},
email: {
type: String,
}
});
export default mongoose.model('User', UserSchema);
const ProfileSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
handle: {
type: String,
},
company: {
type: String,
},
});
export default mongoose.model('Profile', ProfileSchema);
import mongoose from 'mongoose';
const TrustedContactsSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
approvedTrustedContacts: [
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
}
],
...
});
export default mongoose.model('TrustedContacts', TrustedContactsSchema);
I can populate trusted contact by user
const user = await TrustedContacts.findOne({ user: req.user.id }).populate('approvedTrustedContacts.user', ['name', 'email']);
and I got
"user": {
"date": "2019-09-09T07:32:20.174Z",
"_id": "5d76008e4b98e63e58cb34cc",
"approvedTrustedContacts": [
{
"_id": "5d764e411b7476462cf6b540",
"user": {
"_id": "5c5ecaf6134fc342d4b1a9d5",
"name": "User",
"email": "user#gmail.com",
}
},
{
"_id": "5d764e411b7476462cf6b541",
"user": {
"_id": "5c64968cae53a8202c963223",
"name": "User1",
"email": "user1#gmail.com",
}
}
],
"pendingApprovalContacts": [],
"waitingForApprovalContacts": [],
"user": "5d76008e4b98e63e58cb34cb",
}
Expected Output
It is possible to get list of approvedTrustedContacts with profile data
"approvedTrustedContacts": [
{
"_id": "5d764e411b7476462cf6b540",
"user": {
"_id": "5c5ecaf6134fc342d4b1a9d5",
"name": "User",
"email": "user#gmail.com",
},
"handle": "handle",
"company": "test"
},
{
"_id": "5d764e411b7476462cf6b541",
"user": {
"_id": "5c64968cae53a8202c963223",
"name": "User1",
"email": "user1#gmail.com",
},
"handle": "handle1",
"company": "test1"
}
],
Also I have joined 2 collections like this
let result1 = await TrustedContacts.aggregate([
{ $lookup: { from: "profiles", localField: "user", foreignField: "user", as: "approvedTrustedContacts" } },
]);
And I got
{
"_id": "5d76008e4b98e63e58cb34cc",
"approvedTrustedContacts": [
{
"_id": "5d764f551b7476462cf6b542",
"user": "5d76008e4b98e63e58cb34cb",
"handle": "handle",
"company": "test",
},
{
"_id": "5c5ecb17134fc342d4b1a9d6",
"user": "5c5ecaf6134fc342d4b1a9d5",
"handle": "handle1",
"company": "test1",
}
],
"pendingApprovalContacts": [],
"waitingForApprovalContacts": [],
"user": "5d76008e4b98e63e58cb34cb",
}
],
Now I don't know how to polute user to have an output like this:
{
"_id": "5d76008e4b98e63e58cb34cc",
"approvedTrustedContacts": [
{
"_id": "5d764f551b7476462cf6b542",
"user": {
"_id": "5c5ecaf6134fc342d4b1a9d5",
"name": "User",
"email": "user#gmail.com",
},
"handle": "handle"
"company": "test",
},
{
"_id": "5c5ecb17134fc342d4b1a9d6",
"user": {
"_id": "5c64968cae53a8202c963223",
"name": "User1",
"email": "user1#gmail.com",
},
"handle": "handle1",
"company": "test1",
}
],
"pendingApprovalContacts": [],
"waitingForApprovalContacts": [],
"user": "5d76008e4b98e63e58cb34cb",
}
],
My solution
let result = await TrustedContacts.aggregate([
{ $lookup: { from: "profiles", localField: "approvedTrustedContacts.user", foreignField: "user", as: "approvedTrustedContacts.profile" } },
{ $unwind: "$approvedTrustedContacts.profile" },
{ $lookup: { from: "users", localField: "approvedTrustedContacts.profile.user", foreignField: "_id", as: "approvedTrustedContacts.profile.user" } },
{ $unwind: "$approvedTrustedContacts.profile.user" },
{ $group :{
_id: "$_id",
"date": {"$first": "$date"},
"approvedTrustedContacts": {"$push": "$approvedTrustedContacts"},
}}
]);
As an output I have
{
"_id": "5d76008e4b98e63e58cb34cc",
"date": "2019-09-09T07:32:20.174Z",
"approvedTrustedContacts": [
{
"profile": {
"_id": "5c5ecb17134fc342d4b1a9d6",
"skills": [
"test"
],
"date": "2019-02-09T12:42:48.969Z",
"user": {
"_id": "5c5ecaf6134fc342d4b1a9d5",
"data": "2019-02-09T12:42:48.716Z",
"name": "User",
"email": "user#gmail.com",
},
"handle": "handle",
"company": "test",
}
},
{
"profile": {
"_id": "5c6496ebae53a8202c963224",
"skills": [
"qwqwqwqwqw"
],
"date": "2019-02-13T22:11:04.119Z",
"user": {
"_id": "5c64968cae53a8202c963223",
"data": "2019-02-13T22:11:03.807Z",
"name": "User1",
"email": "user1#gmail.com",
},
"handle": "handle1",
"company": "test1",
}
}
]
}
```

Reshaping Documents in $project Stages

For our collection which looks like this:
> db.companies.find().limit(1).pretty() {
"_id": ObjectId("52cdef7c4bab8bd675297d8b"),
"name": "AdventNet",
"permalink": "abc3",
"crunchbase_url": "http://www.crunchbase.com/company/adventnet",
"homepage_url": "http://adventnet.com",
"blog_url": "",
"blog_feed_url": "",
"twitter_username": "manageengine",
"category_code": "enterprise",
"number_of_employees": 600,
"founded_year": 1996,
"deadpooled_year": 2,
"tag_list": "",
"alias_list": "Zoho ManageEngine ",
"email_address": "pr#adventnet.com",
"phone_number": "925-924-9500",
"description": "Server Management Software",
"created_at": ISODate("2007-05-25T19:24:22Z"),
"updated_at": "Wed Oct 31 18:26:09 UTC 2012",
"overview": "<p>AdventNet is now <a href=\"/company/zoho-manageengine\"
title=\"Zoho ManageEngine\" rel=\"nofollow\">Zoho ManageEngine</a>.</p>\n\n<p>F
ounded in 1996, AdventNet has served a diverse range of enterprise IT, networkin
g and telecom customers.</p>\n\n<p>AdventNet supplies server and network managem
ent software.</p>",
"image": {
"available_sizes": [
[
[
150,
55
],
"assets/images/resized/0001/9732/19732v1-max-150
x150.png"
],
[
[
150,
55
],
"assets/images/resized/0001/9732/19732v1-max-250
x250.png"
],
[
[
150,
55
],
"assets/images/resized/0001/9732/19732v1-max-450
x450.png"
]
]
},
"products": [],
"relationships": [{
"is_past": true,
"title": "CEO and Co-Founder",
"person": {
"first_name": "Sridhar",
"last_name": "Vembu",
"permalink": "sridhar-vembu"
}
}, {
"is_past": true,
"title": "VP of Business Dev",
"person": {
"first_name": "Neil",
"last_name": "Butani",
"permalink": "neil-butani"
}
}, {
"is_past": true,
"title": "Usabiliy Engineer",
"person": {
"first_name": "Bharath",
"last_name": "Balasubramanian",
"permalink": "bharath-balasibramanian"
}
}, {
"is_past": true,
"title": "Director of Engineering",
"person": {
"first_name": "Rajendran",
"last_name": "Dandapani",
"permalink": "rajendran-dandapani"
}
}, {
"is_past": true,
"title": "Market Analyst",
"person": {
"first_name": "Aravind",
"last_name": "Natarajan",
"permalink": "aravind-natarajan"
}
}, {
"is_past": true,
"title": "Director of Product Management",
"person": {
"first_name": "Hyther",
"last_name": "Nizam",
"permalink": "hyther-nizam"
}
}, {
"is_past": true,
"title": "Western Regional OEM Sales Manager",
"person": {
"first_name": "Ian",
"last_name": "Wenig",
"permalink": "ian-wenig"
}
}],
"competitions": [],
"providerships": [{
"title": "DHFH",
"is_past": true,
"provider": {
"name": "A Small Orange",
"permalink": "a-small-orange"
}
}],
"total_money_raised": "$0",
"funding_rounds": [],
"investments": [],
"acquisition": null,
"acquisitions": [],
"offices": [{
"description": "Headquarters",
"address1": "4900 Hopyard Rd.",
"address2": "Suite 310",
"zip_code": "94588",
"city": "Pleasanton",
"state_code": "CA",
"country_code": "USA",
"latitude": 37.692934,
"longitude": -121.904945
}],
"milestones": [],
"video_embeds": [],
"screenshots": [{
"available_sizes": [
[
[
150,
94
],
"assets/images/resized/0004/3400/43400v1
-max-150x150.png"
],
[
[
250,
156
],
"assets/images/resized/0004/3400/43400v1
-max-250x250.png"
],
[
[
450,
282
],
"assets/images/resized/0004/3400/43400v1
-max-450x450.png"
]
],
"attribution": null
}],
"external_links": [],
"partners": []
} >
For the below query for our node.js app.
db.companies.aggregate([{
$match: {
"name": "Facebook"
},
$project: {
"_id": 0,
"name": 1,
"people": "$relationships.person.last_name"
}
}])
Gives the following error:
assert: command failed: {
"ok" : 0,
"errmsg" : "A pipeline stage specification object must contain exactly o ne field.",
"code" : 16435 } : aggregate failed
_getErrorWithCode#src/mongo/shell/utils.js:25:13 doassert#src/mongo/shell/assert.js:13:14
assert.commandWorked#src/mongo/shell/assert.js:267:5
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1312:5
#(shell):1:1
2016-09-17T19:20:26.303+0530 E QUERY [thread1] Error: command
failed: {
"ok" : 0,
"errmsg" : "A pipeline stage specification object must contain exactly o ne field.",
"code" : 16435 } : aggregate failed :
_getErrorWithCode#src/mongo/shell/utils.js:25:13 doassert#src/mongo/shell/assert.js:13:14
assert.commandWorked#src/mongo/shell/assert.js:267:5
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1312:5
#(shell):1:1
I'm unable to figure out why?
You are missing some braces for your aggregation pipeline operators. The correct pipeline should be:
db.companies.aggregate([
{ "$match": { "name": "Facebook" } }, /* match pipeline stage */
{ "$project": { /* project pipeline stage */
"_id": 0,
"name": 1,
"people": "$relationships.person.last_name"
} }
])

Resources