skip to Main Content

I have a Mix project and I am trying to insert some data into my local database.

I have added the Mongo dependency to my project, then added the lines of code for connecting to the database and for storing data into it.

{:ok, pid} =
  Mongo.start_link(
    url: "mongodb://localhost:27017/tweet_processor")
    
{:ok, result} =
  Mongo.insert_one(pid, "tweets", tweet_to_insert)

But I keep getting

{:error, %Mongo.Error{
  code: 352,
  host: nil,
  message: "command failed: Unsupported OP_QUERY command: insert"}}

What could be the problem?

4

Answers


  1. I wild guess (out of the shape of commands you’ve shared) that you are using mongodb package. It explicitly states it supports MongoDB versions 2.6÷4.0. I also wild guess you are using MongoDB 5+ backend, which has OP_QUERY explicitly deprecated.

    The driver you use is OSS, and from its source code, one might see that Mongo.insert_one/4 delegates to low-level call which issues OP_QUERY.

    One possibility to fix the issue would be to downgrade MongoDB to v4.0, another (most appreciated by a community) would be to provide a PR to the library, supporting MongoDB 5+.

    Login or Signup to reply.
  2. You can just use this driver which has support for MongoDB 5.x

    Login or Signup to reply.
  3. Update mongo-java-driver version and try again

    <dependency>
       <groupId>org.mongodb</groupId>
       <artifactId>mongo-java-driver</artifactId>
       <version>3.12.2</version>
    </dependency>
    
    Login or Signup to reply.
  4. I was getting the same error. "Unsupported OP_QUERY command: insert" For me this ended up being an issue with Mongoose. After upgrading mongoose to ^6.5.2 it works without issue.

    With NPM you can update the package like so:

    npm install [email protected]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search