So according to MongoDB charts the code I need to replicate the chart is the following (below) however when I copy and paste the code into my NodeJS app it throws an error.
See below.
How do I get this to work in my NodeJS code.
{
"isFetching": false,
"isOpen": true,
"pipeline": [
{
"$match": {
"campaignID": {
"$oid": "629d7fb4b0697d84aa7043ee"
}
}
},
{
"$addFields": {
"__alias_0": {
"$cond": {
"if": {
"$isArray": "$history"
},
"then": {
"$size": "$history"
},
"else": 0
}
}
}
},
{
"$addFields": {
"timeplay": {
"$cond": {
"if": {
"$eq": [
{
"$type": "$timeplay"
},
"date"
]
},
"then": "$timeplay",
"else": null
}
}
}
},
{
"$addFields": {
"__alias_1": {
"year": {
"$year": "$timeplay"
},
"month": {
"$subtract": [
{
"$month": "$timeplay"
},
1
]
},
"date": {
"$dayOfMonth": "$timeplay"
}
}
}
},
{
"$group": {
"_id": {
"__alias_1": "$__alias_1"
},
"__alias_0": {
"$sum": "$__alias_0"
}
}
},
{
"$project": {
"_id": 0,
"__alias_1": "$_id.__alias_1",
"__alias_0": 1
}
},
{
"$project": {
"x": "$__alias_1",
"y": "$__alias_0",
"_id": 0
}
},
{
"$sort": {
"x.year": 1,
"x.month": 1,
"x.date": 1
}
},
{
"$limit": 5000
}
]
}
However when I try and run it I get
error - MongoInvalidArgumentError: Argument "pipeline" must be an array of aggregation stages
Should the code not be working, as this is what MongoDB own system says it is correct code? all I did was copy and paste the code they supplied.
Full code:
import clientPromise from "../../../../lib/mongodb";
import { MongoClient, ObjectId } from 'mongodb'
export default async function overview(req, res) {
//CONNECT
const uri = process.env.MONGODB_URI
let client
let clientPromise
const options = {}
client = new MongoClient(uri, options)
clientPromise = client.connect()
const clients = await clientPromise
const database = clients.db('DATABASE');
const nowplayingstats = await database.collection('NowPlaying');
//END CONNECT
// res.send("YES I WORKED "+total);
const search = await nowplayingstats.aggregate({
"isFetching": false,
"isOpen": true,
"pipeline": [
{
"$match": {
"campaignID": {
"$oid": "629d7fb4b0697d84aa7043ee"
}
}
},
{
"$addFields": {
"__alias_0": {
"$cond": {
"if": {
"$isArray": "$history"
},
"then": {
"$size": "$history"
},
"else": 0
}
}
}
},
{
"$addFields": {
"timeplay": {
"$cond": {
"if": {
"$eq": [
{
"$type": "$timeplay"
},
"date"
]
},
"then": "$timeplay",
"else": null
}
}
}
},
{
"$addFields": {
"__alias_1": {
"year": {
"$year": "$timeplay"
},
"month": {
"$subtract": [
{
"$month": "$timeplay"
},
1
]
},
"date": {
"$dayOfMonth": "$timeplay"
}
}
}
},
{
"$group": {
"_id": {
"__alias_1": "$__alias_1"
},
"__alias_0": {
"$sum": "$__alias_0"
}
}
},
{
"$project": {
"_id": 0,
"__alias_1": "$_id.__alias_1",
"__alias_0": 1
}
},
{
"$project": {
"x": "$__alias_1",
"y": "$__alias_0",
"_id": 0
}
},
{
"$sort": {
"x.year": 1,
"x.month": 1,
"x.date": 1
}
},
{
"$limit": 5000
}
]
}).toArray();
res.json(search)
}
The answer was simple, take everything from pipeline and nothing else.
Code ended up being
[
{
"$match": {
"campaignID": {
"$oid": "629d7fb4b0697d84aa7043ee"
}
}
},
{
"$addFields": {
"__alias_0": {
"$cond": {
"if": {
"$isArray": "$history"
},
"then": {
"$size": "$history"
},
"else": 0
}
}
}
},
{
"$addFields": {
"timeplay": {
"$cond": {
"if": {
"$eq": [
{
"$type": "$timeplay"
},
"date"
]
},
"then": "$timeplay",
"else": null
}
}
}
},
{
"$addFields": {
"__alias_1": {
"year": {
"$year": "$timeplay"
},
"month": {
"$subtract": [
{
"$month": "$timeplay"
},
1
]
},
"date": {
"$dayOfMonth": "$timeplay"
}
}
}
},
{
"$group": {
"_id": {
"__alias_1": "$__alias_1"
},
"__alias_0": {
"$sum": "$__alias_0"
}
}
},
{
"$project": {
"_id": 0,
"__alias_1": "$_id.__alias_1",
"__alias_0": 1
}
},
{
"$project": {
"x": "$__alias_1",
"y": "$__alias_0",
"_id": 0
}
},
{
"$sort": {
"x.year": 1,
"x.month": 1,
"x.date": 1
}
},
{
"$limit": 5000
}
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.