我必须使用Ruby(不是在Rails上,在Notepad ++上)在我的Mongo数据库中插入文档,许多文档都有重复,但有一些修改。
我想编写一个使用json文件的脚本,读取它,通过检查每个文档是否在数据库中没有任何重复,将其导入MongoDB中,如果有重复,我想组合它,如果它包含任何其他信息:
如:
文件1
{ "Name" : "Lila",
"Files":
[
{ "Name": "File3", "Date" : "26-03-2018"}
]
}
文件2
{ "Name" : "Lila",
"Files":
[
{ "Name": "File1", "Date" : "05-11-2017"},
{ "Name": "File2", "Date" : "26-03-2018"},
{ "Name": "File3", "Date" : "26-03-2018"}
]
}
将它们组合起来:
public void CheckingFiveLetter()
{
for (int i = 0; i < convertStoredLetters.Length; i++)
//convertStoredLetters is consonants and vowels which i get at
runtime
{
for (int j = 0; j < convertStoredLetters.Length; j++)
{
for (int k = 0; k < convertStoredLetters.Length; k++)
{
for (int l = 0; l < convertStoredLetters.Length; l++)
{
for (int m = 0; m < convertStoredLetters.Length; m++)
{
if (i != j && i != k && i != l && i != m && j != i && j != k && j != l && j != m && k != i && k != j && k != l && k != m && l != i && l != j && l != k && l != m && m != i && m != j && m != k && m != l)
{
letterword5.Add (convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (m, 1));
letterword5.Add (convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (i, 1));
letterword5.Add (convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (j, 1));
letterword5.Add (convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (k, 1));
letterword5.Add (convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (l, 1));
letterword5.Add (convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (j, 1));
letterword5.Add (convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (k, 1));
letterword5.Add (convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (l, 1));
letterword5.Add (convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (m, 1));
letterword5.Add (convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (i, 1));
}
}
}
}
}
}
//checking if the combination is present in the word dictionary or not
if present add count
for (int m = 0; m < letterword5.Count; m++)
{
bool Checkfi = wordRan.CheckWord (letterword5 [m].ToLower ());
if (Checkfi == true)
{
fiveLetterWord.Add (letterword5 [m].ToLower ());
}
}
fi = fiveLetterWord.Distinct ().ToList ();
wordRan.storeFiveWordCount = fi.Count;
five.text = "Five Possible words : " + fi.Count.ToString ();
letterword5.Clear ();
fiveLetterWord.Clear ();
}
我发现在mongo shell中有可能归功于聚合 - 积累$ mergeObjects,但在Ruby中它似乎不存在。
答案 0 :(得分:2)
您也可以使用ruby中的所有运算符。您需要首先获取基础集合对象。
require 'mongo'
db = Mongo::Connection.new.db("mydb")
coll = db.collection('posts')
coll.aggregate([
{"$project" => {"last_name" => 1, "first_name" => 1 }},
{"$match" => {"last_name" => "Jones"}}
])
这是一个示例管道。您可以将mongo shell上为您工作的相同聚合管道提供给aggregate
。
有关更多信息,请参阅MongoDB Ruby驱动程序文档: http://www.rubydoc.info/gems/mongo/1.8.2/Mongo%2FCollection%3Aaggregate