Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can graaljs not merge Array? #838

Open
k1tano opened this issue Aug 28, 2024 · 1 comment
Open

How can graaljs not merge Array? #838

k1tano opened this issue Aug 28, 2024 · 1 comment
Labels
question Further information is requested

Comments

@k1tano
Copy link

k1tano commented Aug 28, 2024

script like "const avgSendReceiveDiff = db.chatGptMessage.aggregate([
{
$project: {
created_by: 1,
timeDifference: { $subtract: ["$receive_time", "$send_time"] }
}
},
{
$group: {
_id: "$created_by",
avgTimeDifference: { $avg: "$timeDifference" }
}
}
])"
When i use context.eval("js",script)
I expected the "$subtract: ["$receive_time", "$send_time"]" stay unchanged and mongo java driver can parse, but when i extract the string, it was serialize into " $subtract: Array(2)", and mongodb can not parse.
How can i fix this?

@woess
Copy link
Member

woess commented Aug 28, 2024

I suppose you got that from Value.toString()? That string representation is not supposed to be consumed by applications. You can use various Value methods (like getMember(), getMemberKeys(), getArrayElement(), getArraySize()) to access object members and array elements, or use JSON.stringify to convert an object back into a string.

@woess woess added the question Further information is requested label Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants
@woess @k1tano and others