top of page

MongoDB Queries Help | MongoDB basic practice queries | Realcode4you


Database Use: tweets.json




Practical Questions and queries

1. Write a MongoDB query to retrieve all documents from the Tweets collection where user names do not equal "user 30" and "user 40".




Solution 1:

db.Tweets.find({"user.name":{$not: { $in: [ "user 30", "user 40" ] }} }) 

Solution 2:

db.Tweets.find({"user.name":{$nin: [ "user 30", "user 40" ] } }) 


2. Write a MongoDB query that returns all Tweets in which the field “tag” has values. Note that not all the Tweets have tags, obviously.

db.Tweets.find({"tag": {$exists:true}}

3. Write a MongoDB query to display the first 10 Tweets which has the screen name with value "Twitter User" (i.e., "screen_name": "Twitter User").

db.Tweets.find({"user.screen_name":"Twitter User"}).limit(10)

4. Write a MongoDB query to find the Tweets where the Tweet id (i.e., id_str) is greater than 8000000000000000000 but less than 9000000000000000000.

db.Tweets.find({"id_str":{"$gt":"8000000000000000000","$lt":"9000000000000000000"}})

5. Write a MongoDB query to return the "Tweet Id", "name" and "location" fields for those Tweetswhich contain "Thu" as first three letters for its "created_at" field.

db.Tweets.find({"created_at":{"$regex":"Thu"}},{"user.id":1,"user.name":1,"user.location":1})

6. Write a MongoDB query to find the Tweet Id ("id_str") for those Tweets which contain thekeyword “Sydney” in their text.

db.Tweets.find({"text":/Sydney/},{"id_str":1})

7. Write a MongoDB query to count the number of the Tweets whose user id is greater than224499494502 but less than 224499494522.

db.Tweets.find({"user.id":{"$gt":224499494502,"$lt":224499494522}}).count() 

8. Write a MongoDB query to sort all the Tweets in descending order according to the created time("created_at"). Repeat the question and output in ascending order.

db.Tweets.find().sort({"created_at":-1}) 
db.Tweets.find().sort({"created_at":1}) 

9. Write a MongoDB query to find the three earliest tweets (according to created time).

db.Tweets.find().sort({"created_at":1}).limit(3)

10. Write a MongoDB query to update a user’s (user name is ‘user 21’) screen_name as ‘Superman’.

db.Tweets.update({"user.name":"user 21"},{$set:{"user.screen_name":" limit(3) 

11. Write a MongoDB query to remove or delete a user’s information (the user’s screen_name is ‘Superman’)

db.Tweets.remove({"user.screen_name":"Superman"}) db.Tweets.deleteOne({"user.screen_name":"Superman"})

12. Write a MongoDB query to retrieve all documents from the Tweets collection where user id is not 224499494502.


Solution 1:

db.Tweets.find({"user.id":{$ne:224499494502}}) 

Solution 2:

db.Tweets.find({"user.id":{$not:{$eq:224499494502}}}) 


13. Write a MongoDB query to retrieve all documents from the Tweets collection where user names do not equal "user 30" and "user 40".


Solution 1:

db.Tweets.find({"user.name":{$not: { $in: [ "user 30", "user 40" ] }} }) 

Solution 2:

db.Tweets.find({"user.name":{$nin: [ "user 30", "user 40" ] } }) 

14. Write a program in Python, to read the Tweet Dataset from MongoDB. For each tweet, output/display its ‘text’ field in Python console.

from pymongo import MongoClient 
client: MongoClient = MongoClient('127.0.0.1',27017) 
db = client["Practical1"] 
tweets = db["Tweets"] 
for t in tweets.find():   
	print(t["text"] + "\n"


If you have any doubt or query related to MongoDB Database or other Databases then you can get in touch with or send your requirement details to get help in any other task at:


realcode4you@gmail.com

224 views0 comments

Comments


bottom of page