如何仅导入不存在的文档

如何仅导入不存在的文档

本文介绍了如何仅导入不存在的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongo import来导入一堆json,并且我正在寻找一种仅导入不存在的记录的方法(可以由oid检查).我尝试使用--upsert,但它会更新记录,如果它已经存在,我想忽略它.

I am using mongo import in order to import a bunch of jsons and I am looking for a way only to import records that don't exist (can be checked by oid).I tried with --upsert but it updates the record and I want to ignore it completley if it's already there.

有什么想法吗??

推荐答案

mongoimport的默认行为不应是覆盖现有文档:在JS Shell中,我在集合"testimport"中创建了一个文档

The default behavior of mongoimport should not be to overwrite existing documents:In the JS shell, I created a document in the collection "testimport"

> db.testimport.save({_id:1, x:"a"})
> db.testimport.find()
{ "_id" : 1, "x" : "a" }
>

这是文件import.json的内容.它包含2个文档,一个带有唯一的_id,另一个带有重复的_id.

Here are the contents of the file import.json. It contains 2 documents, one with a unique _id, and one with a duplicate _id.

import.json
{_id:1, x:"b"}
{_id:2, x:"b"}

在新的终端窗口中,运行mongoimport:

In a new terminal window, mongoimport is run:

$ ./mongoimport -d test -c testimport import.json -vvvvv
Wed Apr  4 19:03:48 creating new connection to:127.0.0.1
Wed Apr  4 19:03:48 BackgroundJob starting: ConnectBG
Wed Apr  4 19:03:48 connected connection!
connected to: 127.0.0.1
Wed Apr  4 19:03:48 ns: test.testimport
Wed Apr  4 19:03:48 filesize: 29
Wed Apr  4 19:03:48 got line:{_id:1, x:"b"}
Wed Apr  4 19:03:48 got line:{_id:2, x:"b"}
imported 2 objects
$

即使mongoimport的输出表明已导入两个对象,带有_id:1的文档也没有被覆盖.

Even though the output of mongoimport says that two objects were imported, the document with _id:1 has not been overwritten.

> db.testimport.find()
{ "_id" : 1, "x" : "a" }
{ "_id" : 2, "x" : "b" }
>

如果使用--upsert标志,则具有_id:1的文档将被更新:

If the --upsert flag is used, then the document with _id:1 will be updated:

$ ./mongoimport -d test -c testimport import.json -vvvvv --upsert
Wed Apr  4 19:14:26 creating new connection to:127.0.0.1
Wed Apr  4 19:14:26 BackgroundJob starting: ConnectBG
Wed Apr  4 19:14:26 connected connection!
connected to: 127.0.0.1
Wed Apr  4 19:14:26 ns: test.testimport
Wed Apr  4 19:14:26 filesize: 29
Wed Apr  4 19:14:26 got line:{_id:1, x:"b"}
Wed Apr  4 19:14:26 got line:{_id:2, x:"b"}
imported 2 objects
$

在JS shell中,我们可以看到具有_id:1的文档已更新:

In the JS shell, we can see that the document with _id:1 has been updated:

> db.testimport.find()
{ "_id" : 1, "x" : "b" }
{ "_id" : 2, "x" : "b" }
>

这不是您所遇到的行为吗?上面的代码已经使用2.1.1-pre版本进行了测试,但是我不认为mongoimport代码已经改变了一段时间.

Is this not the behavior that you are experiencing? The above was tested with version 2.1.1-pre, but I do not believe that the mongoimport code has changed for a while.

这篇关于如何仅导入不存在的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:48