私はmongodbを使ってツイッタークローンを作成しており、特定のハッシュタグを持つツイートを検索したいと思っています。私は自分のデータベースの文書構造を、次のを思い付いた -各サブ文書の配列に特定のキーワードが含まれている配列内のサブ文書を見つける方法を見つける方法?
{
"_id" : ObjectId("56f88c038c297eb4048e5dff"),
"twitterHandle" : "abhayp",
"firstName" : "Abhay",
"lastName" : "Pendawale",
"emailID" : "[email protected]",
"password" : "278a2c36eeebde495853b14e6e5525fd12074229",
"phoneNumber" : "12394872398",
"location" : "San Jose",
"birthYear" : 1992,
"birthMonth" : 0,
"birthDay" : 1,
"followers" : [
"abhayp",
"deepakp",
"john",
"madhuras",
"nupurs",
"vgalgali"
],
"following" : [
"abhayp",
"abhinavk",
"ankitac",
"anupd",
"arpits"
],
"tweets" : [
{
"tweet_id" : "3f0fe01f8231356f784d07111efdf9d8ead28133",
"tweet_text" : "This new twitter sounds good!",
"created_on" : ISODate("2016-03-13T01:47:37Z"),
"firstName" : "Abhay",
"lastName" : "Pendawale",
"twitterHandle" : "abhayp",
"tags" : [ ]
},
{
"tweet_id" : "4e57b6d7d6b47d69054f0be55c238e8038751d84",
"tweet_text" : "#CMPE273 Node.js is #awesome",
"created_on" : ISODate("2016-03-07T23:16:39Z"),
"firstName" : "Abhay",
"lastName" : "Pendawale",
"twitterHandle" : "abhayp",
"tags" : [
"awesome",
"CMPE273"
]
},
{
"tweet_id" : "e5facd5f37c44313d5be02ffe0a3ca7190affd6b",
"tweet_text" : "Getting an incredible welcome in #Pune #travel #adventure #film #india ",
"created_on" : ISODate("2016-03-07T23:37:27Z"),
"firstName" : "Abhay",
"lastName" : "Pendawale",
"twitterHandle" : "abhayp",
"tags" : [
"adventure",
"film",
"india",
"Pune",
"travel"
]
},
{
"tweet_id" : "f5a735c1f747732f3e04f6cb2c092ff44750c0fd",
"tweet_text" : "The D Day today!\n#TheDDay",
"created_on" : ISODate("2016-03-18T22:24:57Z"),
"firstName" : "Abhay",
"lastName" : "Pendawale",
"twitterHandle" : "abhayp",
"tags" : [ ]
}
]}
私はつぶやきのtags
配列内の「プネ」を持っているツイートを知りたいです。私は次のコマンドを試してみた db.users.find({ "tweets.tags" : {$all: ["Pune"] }}, { tweets : 1 }).pretty();
このコマンドは、自分のつぶやきの1つのタグに 'Pune'というエントリがあるユーザーのすべてのつぶやきを返します。 tags
の配列に「Pune」というエントリを持つツイートだけを検索するにはどうすればよいですか?
注:#Puneをツイートしたユーザーは望んでいません。#Puneを含むすべてのツイートが欲しいです。二重にマークされた問題はこの問題を解決しません。クエリ、次の
実行 - 結果、以下の
db.users.aggregate([{
$match: {
'tweets.tags': 'Pune'
}
}, {
$project: {
tweets: {
$filter: {
input: '$tweets',
as: 'tweet',
cond: {
$eq: ['$$tweet.tags', 'Pune']
}
}
}
}
}]);
リターン -
{ "_id" : ObjectId("56f88c038c297eb4048e5df1"), "tweets" : [ ] }
{ "_id" : ObjectId("56f88c038c297eb4048e5dff"), "tweets" : [ ] }
{ "_id" : ObjectId("56f88c038c297eb4048e5e07"), "tweets" : [ ] }
私が欲しいものは確かではありません! :(
こんにちは、何とか重複した質問私の元の質問を解決することはできません。 – pratiksanglikar