检查mongo集合中是否存在值
我正在使用Node js,并且正在使用mongo(我是绝对的初学者)。现在,我需要具有基本需要看起来像此数组的集合
I'm working in node js and I'm using mongo (Im absolute beginner) . Now I need to have collection that basically needs to look like this array
var keys = ['key1','key2','key3' ]
//这样,我可以轻松地使用indexOf函数检查该数组中是否存在某些值,
var keys = ['key1','key2','key3']
// with this I easily can check if some value I have exist in this array with indexOf function ,
现在我需要进行收集在mongo中,只需要存储用户制作的密钥,并且如果密钥已经存在于集合中,则无需执行任何操作。
Now I need to make collection in mongo that only needs to store keys that user makes and if key already exist in collection it need to do nothing.
//我的密钥看起来像这样,它可以是一个字符串,也可以是字符串数组
//My keys looks something like this , it can be one string or it can be an array of string
Keys = 'home4.car3' or Keys = ['home4.car3','home2.car4']
//我正在像这样插入
// I'm doing insert like this
db.collection('keys',function(err, collection){
collection.insert(Keys, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred'});
} else {
console.log("success");
}
});
});
现在这是当我首先将两个键的数组插入db,然后在那之后发生的事情字符串:
NOW Here is what happened when I first insert array of two keys into db , and then after that one string :
https://gist.github .com / anonymous / fc7730e398519cffde3f
有人能告诉我如何插入该关键字以及如何过滤这些关键字以检查它们是否在集合中吗?
Does anyone can tell me how to to insert for this and how to filter those keys to check if they are into collection?
首先,如果您将文档存储为数组,为什么不将它们存储为数组? ?如果您来自关系数据库的背景,我会看到如何尝试以这种方式存储它,但是在Mongo中,如果它像一个数组,则应将其存储为一个数组。
First off, if you're storing your documents like an array, why don't you just store them as an array? If you come from a relational database background, I can see how you'd be tempted to store it that way, but in Mongo, if it's like an array, it should just be stored as an array.
{
"_id" : ObjectId("52e5361f30f28b6b602e4c7f"),
"0" : "h",
"1" : "o",
"2" : "m"
}
应该是:
{
"_id" : ObjectId("52e5361f30f28b6b602e4c7f"),
"keys" : [ "h", "o", "m" ]
}
在这种情况下,Mongo有一个方便的运算符,称为 $ addToSet
,其作用与 $ push
相同,只是它只会如果不存在,则将其添加到数组中。该操作如下所示:
In this case, Mongo has a handy operator called $addToSet
that will work just like $push
except that it will only add it to the array if it doesn't exist. The operation would look something like this:
collection.update( query, { $addToSet : { 'keys' : 'l' }}, callback );
// add key 'l'
collection.update( query, { $addToSet : { 'keys' : 'm' }}, callback );
// looks in document, sees 'm' in array, and does nothing
EDIT :
通过更新,它将在数组中添加键(如果不存在),但是如果您只想知道它是否存在,我认为最好方式是使用 findOne
:
With the update, it will add the key to the array if it's not there, but if you ONLY want to know if it exists or not, I think the best way would be to use findOne
:
// find the doc by _id and the value you want in keys, returns null if it's not a match
collection.findOne({ _id : 52e5361f30f28b6b602e4c7f, 'keys' : 'l' }, function(err, doc) {
if( doc == null ) {
// do whatever you need to do if it's not there
} else {
// do whatever you need to if it is there
}
db.close();
}
保持插入内容不变,您只需将键
更改为:
To keep your insert as is, you would just need to change Keys
to:
Keys = { 'keys' : [ 'key1', 'key2', 'key3' ] };
insert不需要更改,另外,在您的c中选择,您可能需要将 _id
更改为 username
或添加 username
字段添加到您的文档。
And the insert shouldn't need to change otherwise. Also, in your collection, you might want to change the _id
to be username
or add a username
field to your document.