我想返回流星中的光標(使用MongoDB)。如何在Mongo(流星)中查找找到數組匹配的文檔的查找?
我希望找到的文件(MessageThreads集合),參與者的字段的數組我通過在完全匹配。
下面是一個示例MessageThread文件
{
"_id": "YYSELCguhLurTeyNY",
"creator": "RLmnidY6GypdqDXZu",
"participants": [
"SNhRq4vQpwFBjnTSH",
"RLmnidY6GypdqDXZu"
],
}
當我執行方法addMessage方法,我試圖首先檢查線程是否存在,其中participants
與我傳入的數組完全匹配。此數組將從新消息格式to
字段中制定。
所以,如果我所有的participants
陣列都在文檔participants
字段內但沒有其他字段,則只應返回文檔。例如:如果文件中不存在第三人,但該文件不屬於新消息to
字段,則不應退回該文件。
目前,這是我的查詢,這顯然是過於簡單化。
existingThread = MessageThreads.findOne(participants: participants)
任何指針?謝謝
編輯:我使用的是提供的重複答案有一個問題(但尚未獲準對其他線程評論)如果數組是不同
出於某種原因existingThread
仍然找到一個文檔,但size
是正確的。
編輯2:
下面是我在整個事件的方法,它可以幫助破譯我要去的地方錯誤的代碼。在coffeescript(請原諒,不能在它工作,對不起)。
Meteor.methods
newMessage: (recipientIds, messageContent) ->
if !Meteor.userId()
return false
userId = Meteor.userId()
check recipientIds, [String]
check messageContent, String
participants = recipientIds
participants.push(userId)
participantCount = _.size(participants)
existingThread = MessageThreads.findOne participants:
$size: participantCount
$in: participants
if existingThread?
console.log "Thread exists: #{existingThread._id}"
MessageThreads.update existingThread,
$addToSet: messages: {sender: userId, content: messageContent}
$set: lastUpdated: new Date()
else
newThreadId = MessageThreads.insert
creator: userId
participants: participants
messages: [
{
sender: userId
content: messageContent
createdAt: new Date()
}
]
lastUpdated: new Date()
return newThreadId
開始用谷歌搜索'mongodb的陣列確切match'。這揭示了很多有用的答案。 –
非常感謝,你是對的我顯然不是非常有效地搜索!這個SO問題似乎有一個這樣的答案。 http://stackoverflow.com/questions/29774032/mongodb-find-exact-array-match-but-order-doesnt-matter –
:P好的發現,忘記了答案可以適用於你的情況很好。 – chridam