I am getting the below erro, when I provide the findOne function. It is displaying NULL, couldn't find the solution as there are records with the title.
db.links.findOne({title: "MongoDB TUtor"});
{
"_id" : ObjectId("54f3ea83663952fddd7aaa54"),
"title" : "MongoDB TUtor",
"url" : "mongo.com",
"comment" : "document-oriented db"
}
> db.links.findOne({title: "true"});
null
> db.links.findOne({title: 1});
null
--
Records in links collection
db.links.find().forEach(printjson)
{
"_id" : ObjectId("54f3ea83663952fddd7aaa54"),
"title" : "MongoDB TUtor",
"url" : "mongo.com",
"comment" : "document-oriented db"
}
{
"_id" : ObjectId("54f3ebe7663952fddd7aaa55"),
"title" : "Mongo DB handson",
"url" : "https://university.mongo.com",
"comment" : [
"NoSQL",
"Document-based"
],
"filed_on" : ISODate("2015-03-02T04:47:02.131Z"),
"meta" : {
"browser" : [
"Google Chrome",
"MAC Safari"
],
"version" : "13.4.3.7"
}
}
{
"_id" : ObjectId("54f3f77e663952fddd7aaa5a"),
"title" : "MongoDB_database",
"url" : "mongo.com",
"comment" : "document-oriented db"
}
{
"_id" : ObjectId("54f3f7b7663952fddd7aaa5c"),
"title" : "database",
"url" : "mongo.com",
"comment" : "document-oriented db"
}
{ "_id" : ObjectId("54f3fa5f663952fddd7aaa5d"), "title" : "Rocky" }
{
"_id" : ObjectId("54f3facf663952fddd7aaa5e"),
"title" : "Arnold",
"userId" : ObjectId("54f3fa5f663952fddd7aaa5d")
}
> db.links.find().pretty()
{
"_id" : ObjectId("54f3ea83663952fddd7aaa54"),
"title" : "MongoDB TUtor",
"url" : "mongo.com",
"comment" : "document-oriented db"
}
{
"_id" : ObjectId("54f3ebe7663952fddd7aaa55"),
"title" : "Mongo DB handson",
"url" : "https://university.mongo.com",
"comment" : [
"NoSQL",
"Document-based"
],
"filed_on" : ISODate("2015-03-02T04:47:02.131Z"),
"meta" : {
"browser" : [
"Google Chrome",
"MAC Safari"
],
"version" : "13.4.3.7"
}
}
{
"_id" : ObjectId("54f3f77e663952fddd7aaa5a"),
"title" : "MongoDB_database",
"url" : "mongo.com",
"comment" : "document-oriented db"
}
{
"_id" : ObjectId("54f3f7b7663952fddd7aaa5c"),
"title" : "database",
"url" : "mongo.com",
"comment" : "document-oriented db"
}
{ "_id" : ObjectId("54f3fa5f663952fddd7aaa5d"), "title" : "Rocky" }
{
"_id" : ObjectId("54f3facf663952fddd7aaa5e"),
"title" : "Arnold",
"userId" : ObjectId("54f3fa5f663952fddd7aaa5d")
}
That's not how you do it. The way you are querying this it is looking for an actual document that either has "title": true or "title": 1. Of course, none of your documents meet that condition.
When you want to find out if that field is "actually there" then you use the $exists operator to test for the presence:
db.links.findOne({ "title": { "$exists": true } })
Or naturally with the reverse case when looking for documents that do not have the matching field.
Related
I am pretty new to mongodb. I have 3 levels of documents.
Parent > Child > Child, all having _id field.
{
"_id" : "n2qw5sojs4bajrj",
"Title" : "Mr",
"Instance" : "HQ",
"FirstName" : "ppp",
"LastName" : "uuuu",
"Position" : "BF",
"EmailAddress" : "xxx#ppp.com",
"Requests" : [
{
"_id" : "121",
"Date" : "12/02/2018",
"Status" : "New",
"ApprovedBy" : {
"_id" : "sdfsdf",
"Name" : "MAN"
},
"PPE" : [
{
"_id" : "121",
"Code" : "PPE",
"Status" : "New",
"Title" : "Trousers",
"Type" : "STD",
"Size" : "10111116",
"Qty" : 1,
"LostDamage" : {
"Reason" : "asdaD",
"Location" : "Station",
"Damage" : "Damged"
}
},
{
"_id" : "122",
"Code" : "PPEOPP",
"Status" : "New",
"Title" : "TrousASDASDASDers",
"Type" : "STD",
"Size" : "10111116",
"Qty" : 1,
"LostDamage" : {
"Reason" : "asdaD",
"Location" : "Station",
"Damage" : "Damged"
}
}
]
}
]
}
I would like find out how to delete the last PPE Element (Parent > Request > PPE) by the _id column.
So I would to delete the following child:
{
"_id" : "121",
"Code" : "PPE",
"Status" : "New",
"Title" : "Trousers",
"Type" : "STD",
"Size" : "10111116",
"Qty" : 1,
"LostDamage" : {
"Reason" : "asdaD",
"Location" : "Station",
"Damage" : "Damged"
}
}
Any tips / help would be great.
Thanks
Paul
You can use MongoDb $pop to remove your last element from array. Like this go to https://docs.mongodb.com/manual/reference/operator/update/pop/
db.collection.update({id:1}, { $pop:{ 'request.PPE':1}});
[ "_id" : ObjectId("59d1f4d0539f772aacf90f61"),
"user_id" : 1222.0,
"user_name" : "jadenn",
"email" : "jdkdkdjjjjjjjjjjjjjjjjjjjh#j.com",
"phone" : 333333333.0,
"image_ref" : "static/image/123IMG_20170917_232813.jpg",
"stories" : [
{
"title" : "accident",
"description" : "kjsbskns",
"longitude" : 19.2,
"latitude" : 81.4,
"location" : "isl",
"date" : 12.0,
"reactions" : [
{
"type" : "seen",
"date" : "1234",
"user_id" : 123.0
},
{
"type" : "seen",
"date" : "1234",
"user_id" : 123.0
}
],
"comments" : [
{
"comment" : "djksnjknfkjguye",
"date" : 123.0,
"user_id" : 123.0
},
{
"comment" : "this is the accident case ",
"date" : 123.0,
"user_id" : 123.0
}
],
"reports" : [
{
"description" : "uye",
"date" : 123.0,
"user_id" : 1232321.0
}
],
"catagory_name" : [
{
"type" : "uye"
}
]
}
]
}
Here is my document I want a query which will return only list of stories. Not a complete document of user. I searched but could not find the solution. Every solution return complete document by applying "Where condition as MYSQL"]1
You need to project on stories key from your document as below:
db.youCollection.find({}, {stories:1, _id:0})
I am having trouble with the $group stage in my aggregation. I want to group all the "recentPlays.quiz" values together and count the repeated values, so the end result I want from the aggregation is two fields: the quiz object and the total. In this case it would be something like:
{
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
},
"count": 1
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"count": 1
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
},
"count": 4
}
]
}
Here is the aggregation I have so far:
db.users.aggregate([
{$match: { "recentPlays.date": {$gte:twentyFourHrsAgo}}},
{$project: {"recentPlays.quiz":1, _id:0}}
]).pretty();
Here is that aggregation's output:
MongoDB shell version: 3.2.1
connecting to: videoQuiz
{
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
}
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
}
]
}
Here is the entire collection:
MongoDB shell version: 3.2.1
connecting to: videoQuiz
{
"_id" : ObjectId("580f7be62c6fd3c8065577f5"),
"user" : "blue-tester1",
"email" : "aslfjjcc#lkcjasdc.com",
"createdAt" : ISODate("2016-10-25T15:36:06.933Z"),
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
},
"score" : "0",
"date" : ISODate("2016-10-25T15:36:27.546Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"score" : "100",
"date" : ISODate("2016-10-25T15:37:09.142Z")
}
],
"mostRecentQuiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"mostRecentQuizTime" : ISODate("2016-10-25T15:37:09.142Z"),
"plays" : 2
}
{
"_id" : ObjectId("580a5dea650296d808082e65"),
"user" : "red-tester3",
"email" : "aldkdk#ccc.com",
"createdAt" : ISODate("2016-10-21T18:26:50.870Z"),
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP PLAYED QUIZ - Today"
},
"score" : "0",
"date" : ISODate("2016-10-21T18:27:16.292Z")
},
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP LIKED QUIZ - TODAY"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:27:32.788Z")
},
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP LIKED QUIZ - TODAY"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:27:44.497Z")
},
{
"quiz" : {
"author" : "Bertram",
"title" : "frfrf"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:28:43.893Z")
},
{
"quiz" : {
"author" : "Bertram",
"title" : "Here We Go With the New Thing"
},
"score" : "0",
"date" : ISODate("2016-10-21T18:43:51.205Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Presidents of the United States"
},
"score" : "0",
"date" : ISODate("2016-10-23T00:53:29.167Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Presidents of the United States"
},
"score" : "0",
"date" : ISODate("2016-10-23T00:53:44.815Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 1"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:50:55.355Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:52:33.210Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Here Is a New Title"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:58:53.683Z")
}
],
"mostRecentQuiz" : {
"author" : "red-tester3",
"title" : "Here Is a New Title"
},
"mostRecentQuizTime" : ISODate("2016-10-23T23:58:53.683Z"),
"plays" : 10,
"likedQuizzes" : [
{
"title" : "TOP LIKED QUIZ - TODAY",
"author" : "red-tester2",
"date" : ISODate("2016-10-21T18:27:34.893Z")
},
{
"title" : "frfrf",
"author" : "Bertram",
"date" : ISODate("2016-10-21T18:28:45.863Z")
},
{
"title" : "Here We Go With the New Thing",
"author" : "Bertram",
"date" : ISODate("2016-10-21T18:43:53.148Z")
}
],
"createdQuizzes" : [
{
"title" : "Yeah Here We Go",
"id" : ObjectId("580a63f274b9a89c061f973e")
},
{
"title" : "Z Alpha",
"id" : ObjectId("580a641474b9a89c061f973f")
},
{
"title" : "Tags Limit Test",
"id" : ObjectId("580a6bda8d8049ac0bc1df2e")
},
{
"title" : "Tags Limit test2",
"id" : ObjectId("580a6bf98d8049ac0bc1df2f")
},
{
"title" : "Presidents of the United States",
"id" : ObjectId("580c09d28d8049ac0bc1df30")
},
{
"title" : "Creation Test 1",
"id" : ObjectId("580d4cca8d8049ac0bc1df31")
},
{
"title" : "Creation Test 2",
"id" : ObjectId("580d4d2d8d8049ac0bc1df32")
},
{
"title" : "Here Is a New Title",
"id" : ObjectId("580d4ead8d8049ac0bc1df33")
}
]
}
Thanks in advance for any guidance. Please excuse the dummy text in these documents, it is for testing purposes only.
This will be a two step process. The first step is to $unwind the "recentPlays" array. The second step is to $group by "recentPlays.quiz".
For example:
db.users.aggregate([
{ "$match" : { "recentPlays.date": { "$gte" : twentyFourHrsAgo}}},
{ "$project" : {"recentPlays.quiz":1, _id:0}},
{ "$unwind" : "$recentPlays" },
{ "$group" : { "_id" : "$recentPlays.quiz", "total" : { "$sum" : 1 } } }
]).pretty();
I have a table in mongo db called documents
{
"_id" : ObjectId("565a7f1"),
"_class" : "Document", "name" :
"Book Template",
"location" : "/Book Template.pdf",
"type" : DBRef("types", ObjectId("3eaabf5"))
},
{
"_id" : ObjectId("565a7f2),
"_class" : "Document",
"name" : "Sample Contract",
"location" : "/Sample Contract.pdf",
"type" : DBRef("types", ObjectId("3eaabf5"))
},
{
"_id" : ObjectId("565a7f3"),
"_class" : "Document",
"name" : "Clustering with RabbitMQ",
"location" : "/Clustering with RabbitMQ.txt",
"type" : DBRef("types", ObjectId("3eaabf6"))
}
and then I have a table called types
{
"_id" : ObjectId("3eaabf5"),
"_class" : "Type",
"name" : "PDF",
"description" : "Portable Document Format",
"extension" : ".pdf"
};
{
"_id" : ObjectId("3eaabf6"),
"_class" : "Type",
"name" : "NOTE",
"description" : "Text Notes",
"extension" : ".txt"
},
{
"_id" : ObjectId("3eaabf7"),
"_class" : "Type",
"name" : "WEB",
"description" : "Web Link",
"extension" : ".url"
}
I want to query the documents table for all documents which have an extension of PDF.
So I wrote the following queries... but everything returns empty results.
>db.documents.find({"types.$id":"3eaabf5"})
>
>db.documents.find({"types.name":"PDF"})
>
You may try this
db.documents.find({"type.$id":ObjectId("3eaabf5")}
i'm looking to get a specific query using the aggregation framework of mongoDB. I think i need the $group and $addToSet operators but i'm confused about the right query to use.
This is the article collection:
/* 0 */
{
"_id" : 4,
"author" : "Kevin Vanhove",
"book" : {
"order" : 500,
"title" : "HTML",
"url" : "html"
},
"chapter" : {
"img" : "navChapter-logo",
"order" : 500,
"title" : "W3C",
"url" : "w3c"
},
"featured" : 0,
"heading" : [
{
"title" : "title1",
"_id" : ObjectId("53130fb8b9b9f573a877401d")
},
{
"title" : "title2",
"_id" : ObjectId("53130fb8b9b9f573a877401c")
}
],
"intro" : "Some intro text",
"title" : "Internet with and without the W3C",
"url" : "internet-with-and-without-the-W3C"
}
/* 1 */
{
"_id" : 1,
"author" : "Kevin Vanhove",
"book" : {
"order" : 500,
"title" : "Javascript",
"url" : "javascript"
},
"chapter" : {
"img" : "navChapter-logo",
"order" : 500,
"title" : "Functions",
"url" : "functions"
},
"featured" : 1,
"heading" : [
{
"title" : "Parts of a function",
"_id" : ObjectId("53130e0cd8517b65a614c1ab")
},
{
"title" : "Something else",
"_id" : ObjectId("53130e0cd8517b65a614c1aa")
}
],
"intro" : "Some intro text",
"title" : "A visual illustration of the JS function",
"url" : "a-visual-illustration-of-the-javascript-function"
}
/* 2 */
{
"_id" : 2,
"author" : "Kevin Vanhove",
"book" : {
"order" : 500,
"title" : "Javascript",
"url" : "javascript"
},
"chapter" : {
"img" : "navChapter-logo",
"order" : 300,
"title" : "Variables",
"url" : "variables"
},
"featured" : 1,
"heading" : [
{
"title" : "Global vs local",
"_id" : ObjectId("53130ea8a9dc9c28a77ea28a")
},
{
"title" : "Variable hoisting",
"_id" : ObjectId("53130ea8a9dc9c28a77ea289")
},
{
"title" : "The scope chain",
"_id" : ObjectId("53130ea8a9dc9c28a77ea288")
}
],
"intro" : "Some intro text",
"title" : "How variable scope works in javascript",
"url" : "how-variable-scope-works-in-javascript"
}
/* 3 */
{
"__v" : 0,
"_id" : 3,
"author" : "Kevin Vanhove",
"book" : {
"order" : 500,
"title" : "Javascript",
"url" : "javascript"
},
"chapter" : {
"img" : "navChapter-logo",
"order" : 600,
"title" : "Functions",
"url" : "functions"
},
"featured" : 0,
"heading" : [
{
"title" : "title1",
"_id" : ObjectId("53130f60f0de2506a81e2d62")
},
{
"title" : "title2",
"_id" : ObjectId("53130f60f0de2506a81e2d61")
}
],
"intro" : "Some intro text",
"title" : "Javascript closures, in depth",
"url" : "Javascript-closure-in-depth"
}
I need to have all the 'unique' books and their 'unique' chapters (no duplicates), so i use this query:
/*
db.articles.aggregate({$group : {_id : "$book.title", chapters:{$addToSet:"$chapter.title"}}})
*/
This gives me this result:
/* 0 */
{
"result" : [
{
"_id" : "Javascript",
"chapters" : [
"Variables",
"Functions"
]
},
{
"_id" : "HTML",
"chapters" : [
"W3C"
]
}
],
"ok" : 1
}
That is almost what i want but not completely. What actually need is this:
/* 0 */
{
"result" : [
{
"_id" : "Javascript",
"chapters" : [
{
"img" : "navChapter-logo",
"order" : 600,
"title" : "Functions",
"url" : "functions"
},
{
"img" : "navChapter-logo",
"order" : 300,
"title" : "Variables",
"url" : "variables"
}
]
},
{
"_id" : "HTML",
"chapters" : [
{
"img" : "navChapter-logo",
"order" : 500,
"title" : "W3C",
"url" : "w3c"
}
]
}
],
"ok" : 1
}
So i need all the unique books with their unique chapters, but i also want the extra fields added like "order" and "url". The query i'm using at the moment only gives me the chapter titles.
Update:
I also tried: $addToSet:"$chapter" instead of $addToSet:"$chapter.title"...
But now i get duplicates on the chapter.title field. I should get only 2 distinct chapters in book 'javascript', and now i get 3 chapters (1 duplicate)
You can use $group to aggregate by book and chapter.
db.articles.aggregate(
{$group : {_id : {t:"$book.title",c:"$chapter.title"},
img:{$first:"$chapter.img"},
url:{$first:"$chapter.url"},
order:{$sum:"$chapter.order"}
}})
In case order was important I kept it and added them across the "duplicate" chapters for same book.