I just surf documentation about mongodb and just try to use aggregation and when my query contains { $project: { aw_product_id: 1, _id: 0} } section I got error, section $project everything looks correct, column aw_product_id present in my model, I try to changed to another column but still faced with the same error. What I'm doing wrong ?
db.version() -> 4.4.0
db.getCollection('AwinProduct').aggregate(
[
{ $match: { $text: { $search: "Sko Nike Classic Cortez för män -Svart", $language: 'sv' } } },
{ $sort: { score: { $meta: "textScore" } } },
{ $project: { aw_product_id: 1, _id: 0} }
]
)
nad faced with that
ailed to execute script.
Error: command failed: {
"ok" : 0,
"errmsg" : "FieldPath field names may not start with '$'.",
"code" : 16410,
"codeName" : "Location16410"
} : aggregate failed
Details:
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:534:17
assert.commandWorked@src/mongo/shell/assert.js:618:16
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1062:12
DBCollection.prototype.aggregate@:1:355
@(shell):1:1
I did the same for another collection and for new collection and faced with the same error
I was try use qoutes for fields but still the same error
db.getCollection('AwinProduct').aggregate(
[
{ $match: { $text: { $search: "Sko Nike Classic Cortez för män -Svart", $language: 'sv' } } },
{ $sort: { score: { $meta: "textScore" } } },
{ $project: { "currency": 1, _id: 0} }
]
)
I use mongo in docker structure, this my docker-compose for this img
mongodb:
image: mongo:latest
container_name: mongodb
hostname: mongodb
volumes:
- ./docker/mongodb/mongod.conf:/etc/mongod.conf
- ./docker/mongodb/initdb.d/:/docker-entrypoint-initdb.d/
- ./data/momgo/:/data/db/
- ./docker/mongodb/data/log/:/var/log/mongodb/
- ./docker/mongodb/home:/home/mongodb
env_file:
- .env
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
ports:
- "27017:27017"
command: ["-f", "/etc/mongod.conf"]
networks:
- php
I faced the same problem and I may have some elements for a solution. There seem to be a problem with the $sort operator. Try putting it after your $project operator.
In my situation, there was the same error with the following pipeline : a $sort, then an $unwind, then a $project. The error disappears if the $sort is placed after the $unwind, whether the $unwind is placed after the $project or not.
I don't fully understand the situation but it looks like there is some obscure interaction between $sort and $project when there is a third operator involved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With