shuba.ivan
shuba.ivan

Reputation: 4071

Mongodb 4.4.0 FieldPath field names may not start with

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

Upvotes: 3

Views: 13508

Answers (2)

turivishal
turivishal

Reputation: 36114

After lot of research i found a below instruction in general terms of find and aggregation functions text-score-metadata-meta-textscore under Usage In Projection section,

The { $meta: "textScore" } expression can be a part of the projection document to include the text score metadata.

The $meta expression can be present in either an inclusion or an exclusion projection.

If you set the expression to a field name that already exists in the document, the projected metadata value overwrites the existing value.

Above the bold sentence clears, if you use $project stage after $sort stage the score or text index field that you created the text index should be inclusion or an exclusion,

As per your try the solution:

I don't the text index field name, but i would predict the field name as name,

db.getCollection('AwinProduct').createIndex({ name: "text" });
db.getCollection('AwinProduct').aggregate(
   [
     { $match: { $text: { $search: "Sko Nike Classic Cortez för män -Svart", $language: 'sv'  } } },
     { $sort: { score: { $meta: "textScore" } } },
     { 
       $project: { 
         "_id", "$$REMOVE",
         "name": "$$REMOVE",
         "aw_product_id": 1
     }
   ]
)

There is other way as well as per @Alexandre Masseron's answer, I would recommend this way

  • swap $project and $sort stage,
db.getCollection('AwinProduct').aggregate(
   [
     { $match: { $text: { $search: "Sko Nike Classic Cortez för män -Svart", $language: 'sv'  } } },
     { $project: { aw_product_id: 1, _id: 0 } },
     { $sort: { score: { $meta: "textScore" } } }
   ]
)

Upvotes: 2

Alexandre Masseron
Alexandre Masseron

Reputation: 21

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.

Upvotes: 2

Related Questions