Why to use slug field in mongodb document? - mongodb

I tried to create category hierarchy.So,I refer this link (https://docs.mongodb.com/ecosystem/use-cases/category-hierarchy/) link given number example how to create category hierarchy .but I have a doubt I this example is used slug field.why to use slug I could not understand can you please give solution.
Example Document
{ "_id" : ObjectId("4f5ec858eb03303a11000002"),
"name" : "Modal Jazz",
"parent" : ObjectId("4f5ec858eb03303a11000001"),
"slug" : "modal-jazz",
"ancestors" : [
{ "_id" : ObjectId("4f5ec858eb03303a11000001"),
"slug" : "bop",
"name" : "Bop" },
{ "_id" : ObjectId("4f5ec858eb03303a11000000"),
"slug" : "ragtime",
"name" : "Ragtime" } ]
}
what is slug ?
Which purpose using in document ?
why to use slug field ?

Slug field is URL shortcut to get document or sub/document is simple way.
Slug is used in document to make easy get document from URL
For example, typing http://**host**/modal-jazz you should get document that has "modal-jazz" as 'slug'.
I hope i helped you a bit.

Related

Mongodo db delete documents between custom fields

I have below documents in mongodb, am trying to delete the documents based on the referenceId field between the values X0000000005 and X00000000010, I couldnt find any articles for deleting mongo documents based on custom field, can someone please help me to do this deletion if its possible?
{
"_id" : ObjectId("5a0f13ad0a83924b84d16b7d"),
"senderId" : "783",
"clientId" : "146196",
"referenceId" : "X00000000001",
"file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}
{
"_id" : ObjectId("5a0f13ad0a83924b84d16b7e"),
"senderId" : "783",
"clientId" : "146196",
"referenceId" : "X00000000002",
"file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}
.
.
.
.
.
.
{
"_id" : ObjectId("5a0f13ad0a83924b84d16b7f"),
"senderId" : "783",
"clientId" : "146196",
"referenceId" : "X00000000020",
"file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}
The following simple query should work:
db.collection.remove({"referenceId":{$gte:"X00000000005"}, "referenceId":{$lte:"X00000000010"}})
You might want to run a find() using the same filter first in order to make sure that the delete() will affect the right records. That'd obviously be this then:
db.collection.find({"referenceId":{$gte:"X00000000005"}, "referenceId":{$lte:"X00000000010"}})
Also, depending on the exact definition of your
between the values X0000000005 and X00000000010
you might need to swap the $lte and $gte operators out for something else ($gt and/or $lt).
db.collection.remove({"referenceId": {"$lte": "X00000000010", "gte": "X0000000005"}})

Automatically update lastModified attribute in mongoDB document on change - Meteor

I have a lastModified attribute in my games collection. At the moment I have to manually update the lastModified attribute on every change (current timestamp).
Is it possible to update this attribute automatically when other attributes of the document will be changed?
I use Meteor 1.0.
EDIT:
Sample document:
{ "controllerId" : "ACycCfrQuTtuMwjuJ", "body" : "ABC", "userId" : "5iE4P8HPoRCSHe6k8", "lastModified" : ISODate("2015-01-06T15:48:49.346Z"), "_id" : "CQbL49FP9rZkSF7yh" }
When changing attribute body it should be like this:
{ "controllerId" : "ACycCfrQuTtuMwjuJ", "body" : "XYZ", "userId" : "5iE4P8HPoRCSHe6k8", "lastModified" : ISODate("2015-01-06T19:41:19.888Z"), "_id" : "CQbL49FP9rZkSF7yh" }
lastModified should automatically be updated when one of the other attributes changes.
As explained here, you can abuse the deny mechanism to do this for you:
Games.deny({update: function(userId, doc) {
doc.lastModified = new Date();
return false;
}})
Alternatively you can use collection hooks. Your exact use case is the example for before.update.

I have big database on mongodb and can't find and use my info

This my code:
db.test.find() {
"_id" : ObjectId("4d3ed089fb60ab534684b7e9"),
"title" : "Sir",
"name" : {
"_id" : ObjectId("4d3ed089fb60ab534684b7ff"),
"first_name" : "Farid"
},
"addresses" : [
{
"city" : "Baku",
"country" : "Azerbaijan"
},{
"city" : "Susha",
"country" : "Azerbaijan"
},{
"city" : "Istanbul",
"country" : "Turkey"
}
]
}
I want get output only all city. Or I want get output only all country. How can i do it?
I'm not 100% about your code example, because if your 'find' by ID there's no need to search by anything else... but I wonder whether the following can help:
db.test.insert({name:'farid', addresses:[
{"city":"Baku", "country":"Azerbaijan"},
{"city":"Susha", "country":"Azerbaijan"},
{"city" : "Istanbul","country" : "Turkey"}
]});
db.test.insert({name:'elena', addresses:[
{"city" : "Ankara","country" : "Turkey"},
{"city":"Baku", "country":"Azerbaijan"}
]});
Then the following will show all countries:
db.test.aggregate(
{$unwind: "$addresses"},
{$group: {_id:"$country", countries:{$addToSet:"$addresses.country"}}}
);
result will be
{ "result" : [
{ "_id" : null,
"countries" : [ "Turkey", "Azerbaijan"]
}
],
"ok" : 1
}
Maybe there are other ways, but that's one I know.
With 'cities' you might want to take more care (because I know cities with the same name in different countries...).
Based on your question, there may be two underlying issues here:
First, it looks like you are trying to query a Collection called "test". Often times, "test" is the name of an actual database you are using. My concern, then, is that you are trying to query the database "test" to find any collections that have the key "city" or "country" on any of the internal documents. If this is the case, what you actually need to do is identify all of the collections in your database, and search them individually to see if any of these collections contain documents that include the keys you are looking for.
(For more information on how the db.collection.find() method works, check the MongoDB documentation here: http://docs.mongodb.org/manual/reference/method/db.collection.find/#db.collection.find)
Second, if this is actually what you are trying to do, all you need to for each collection is define a query that only returns the key of the document you are looking for. If you get more than 0 results from the query, you know documents have the "city" key. If they don't return results, you can ignore these collections. One caveat here is if data about "city" is in embedded documents within a collection. If this is the case, you may actually need to have some idea of which embedded documents may contain the key you are looking for.

mongodb: taking a set of keys from one collection and matching with another

I'm new to mongodb and javascript, and have been reading the manual, but I can't seem to put the pieces together to solve the following problem.. I was wondering if you can kindly help.
I have two collections "places" and "reviews".
One document in "places" collection is as follows:
{
"_id" : "004571a7-afe4-4124-996e-b6ec779db494",
"name" : "wakawaka place",
"address" : {
"address" : "12 ad avenue",
"city" : "New York",
},
"review" : [
{
"id" : "i32347",
"review_list" : [
"r123456",
"r123457"
],
}
]
}
The "review" array can be empty for some documents.
And in the "reviews" collection, every document in the collection represents a review:
{
"_id" : ObjectId("53c913689c8e91a5a9c4047f"),
"user_id" : "useridhere",
"review_id" : "r123456",
"attraction_id" : "i32347",
"content" : "review content here"
}
What I would like to achieve is, for each place that has reviews, get the content of each review from the "review" collection and store them together in another new collection.
I'd be grateful for any suggestions on how to go about this.
Thanks

Retrieving only the relevant part of a stored document

I'm a newbie with MongoDB, and am trying to store user activity performed on a site. My data is currently structured as:
{ "_id" : ObjectId("4decfb0fc7c6ff7ff77d615e"),
"activity" : [
{
"action" : "added",
"item_name" : "iPhone",
"item_id" : 6140,
},
{
"action" : "added",
"item_name" : "iPad",
"item_id" : 7220,
}
],
"name" : "Smith,
"user_id" : 2
}
If I want to retrieve, for example, all the activity concerning item_id 7220, I would use a query like:
db.find( { "activity.item_id" : 7220 } );
However, this seems to return the entire document, including the record for item 6140.
Can anyone suggest how this might be done correctly? I'm not sure if it's a problem with my query, or with the structure of the data itself.
Many thanks.
You have to wait the following dev: https://jira.mongodb.org/browse/SERVER-828
You can use $slice only if you know insertion order and position of your element.
Standard queries on MongoDb always return all document.
(question also available here: MongoDB query to return only embedded document)