Mongodump specific collection but only include specific documents

I need to use mongodump on one collection in my database, but I only want to include specific documents in the dump. I can only reference each document by their title (a string, which are all different values) and the documents do not share a common value. I wrote a “—query” in extended JSON that utilized the “$in” operator and listed each “title”, but I get a JSON error. Is there a way to accomplish this task?

Hi @Seolera3, and welcome to the MongoDB community forums! :wave:

Can you provide the error you’re getting? With that we might be able to help you out.

1 Like

Hi @Doug_Duncan I appreciate the timely response. The error I’m getting is “Failed: error parsing query as Extended JSON: invalid JSON input”

My input: -q=‘{“title”: {“$in”: [“title_A”, “title_B”, “title_C”, ”title_D”, “title_E”]}}’

Can you please show a screenshot with the command you’re using (or a subset that causes the error) and the error?

I just did a test and I’m not getting an error:

One thing to note here is that the input you’ve provided has fancy quotes instead of normal quotes. This could be a side effect of pasting into the forums as any quote typed in as normal text will get converted to the fancy quote version. :frowning:

In the future you can prevent that by using preformatted text (either put your element in a set of single backticks, or using the

image

icon.

For longer code block, you can use a format similar to the following:

```
mongodump -d database \
    -c colleciton \
    -q='{"title": {"$in": ["title_A", "title_B", "title_C", "title_D", "title_E"]}}'
```

This allows for the text to be displayed as it was typed and removes any forum formatting issues.

2 Likes

I currently cannot provide a screenshot but my entire command looks like the following:


mongodump --uri="mongodb://localhost:27017"  --out="./collection_dump" --db="db_2" --collection="collection_A"
-q=‘{"title": {"$in": ["title_A”, “title_B”, “title_C", “title_D”, “title_E”]}}’

I’m also working on a windows computer and running this in the command prompt.

Ok, I was able to reproduce this on Windows, I use a MacBook for testing and Linux for production MongoDB and found out the following things about running mongodump in Windows cmd.

If I use the query in the format you have pasted it above (notice that some strings are not in red which means you’ve still get fancy quotes in the string and that will cause problems):

 -q='{"title": {"$in": ["title_A”, “title_B”, “title_C", "title_D", "title_E"]}}'

I get the following error:

It seems that mongodump on Windows does not like a parameter in single quotes to have spaces. So I removed them:

Now I get the same error that you are getting. :frowning:

Let’s try putting the parameter in double quotes, which means we have to escape all of the JSON keys and values:

This finally let me use mongodump in Windows. Note that I am able to have spaces to break up the query so it’s a little easier to read. One my think that maybe I can double quote the outer JSON and then use single quotes around the key/value pairs, but that throws the same message you’re currently seeing:

This seems to be something that is Windows related. Note that I see similar issues running mongo with ---eval:

It looks like you have to jump through hoops on Windows, unless there is someone that works on Windows more than I do that knows other tricks.

Note that I don’t have any problems with using single quotes and spaces in the version I run on my Mac:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.