I havent really ran into a error, but I was wondering. MongoDB calls each thing inside a collection a "document" I was wondering what you call the things inside a document.
Ok, after looking for a long time. This is what you call the things inside a document. Its called a field.
#this entire thing is called a document.
#The fields in this document are: name, age, status, and groups.
#The values in this document are: "sue", 26, "A", and ["news", "sports"]
Hope this helps anyone who had the same question as me!
{
name: "sue",
age: 26,
status "A",
groups: ["news", "sports"]
}
Related
Hello Everybody i'm so sorry to ask but i've been trying to look for an answer for MONTHS, and im already desperate because i can't seem to find what i need till the point that i'm almost giving up with NoSQL.
So here's my Schema.
I have States of a Country and they have their non-repeatable ISO CODE,
{
code: 'US-NY',
name: 'New York',
cities: [ ]
},
etc ..
.. however inside the state (nested objects) we have cities:
cities:[
{
code: 'new-york',
name:'New York',
},
{
code: 'bufalo',
name:'Búfalo',
},
]
So when i add a document, i want to avoid duplicated nested objects (cities) with the same code (example: 'bufalo') within the same document (state).
BUT i could use 'bufalo' in another document (state).
When i set an index to cities.code to be Unique, it applies in the whole collection, and i want to use that code in another document.
I would like to repeat 'bufalo' city code in another document (state), but i dont want it to be repeated in the same document.
How could i archive this? Thank you so much for your kind help, i will be checking this post desperately.
Thanks
Alan D.
Lets say I have a document structured like that :
datas: {
foo: {
...
keytoupdate: [...]
},
whatever: {
...
keytoupdate: [...]
},
anystring: {
...
keytoupdate: [...]
},
...: {
...
keytoupdate: [...]
}
}
I know that :
Each direct child property of the "datas" document has a "keytoupdate" field.
The direct child properties of the "datas" document varies from case to case: not necessarily the same name, neither the same number.
I want to update each "keytoupdate" fields, no matter how many of them there are.
The question is: How can I do that ? Is there any magic operator like $ that does the same job for Array ?
Thank you !
I'll answer my own question : there is no way to do that, we can't play with dynamic keys, just forget about it ! But there are 2 workarounds :
The best solution, as suggested by #chridam, is to redesign the schema to make an array of objects, where the keys are parts of the arrays, you can see this question for more details.
If you can't, the other (but not good) solution is to make a request for each field that might be in your document, instead of trying to do this in one request. This is a very bad solution, especially if your document may have lots of fields, and you have to known which fields that could be in your documents. This is a bad solution, absolutely not optimized, but it has the merit of being simple to implement
In My social network I want to get the feed for member A , member A is following lets say 20 category/member.
when a category/member(followed by member A) do an activity it is inserted into a collection called recent_activity :
{
"content_id": "6", // content id member A is following
"content_type_id": "6",// content type (category , other member)
"social_network_id": "2", // the action category did (add/like/follow)
"member_id": "51758", //Member A
"date_added": ISODate("2014-03-23T11:37:03.0Z"),
"platform_id": NumberInt(2),
"_id": ObjectId("532ec75f6b1f76fa2d8b457b"),
"_type": {
"0": "Altibbi_Mongo_RecentActivity"
}
}
I want when member A login into the system to get last 10 activities for the categories/member
my problem :
How to get Only 10 activities for all categories/members.
It is better to do it in one query or to do a for loop.
For this use case, I'd suggest to invert the logic and keep a separate object of the last 10 activities for member A that is kept up-to-date all the time. While that solution is more write-heavy, it makes reading trivially simple and it can be extended very easily. I'd like to blatantly advertise a blog post I wrote a while ago about news feeds with mongodb which outlines this approach.
This 'fan-out' approach might seem overly complex at first, but when you think about importance filtering / ranking (a la facebook), push messages for particularly important events (facebook, twitter) or regular digest emails (practically all), you will get one location in your code to perform all this logic.
I think I commented that T'm not really seeing the selection criteria. So if you are "outside" of a single collection, then you have problems. But if your indicated fields are the things you want to "filter" by, then just do this:
db.collection.find({
"social_network_id": "2",
"content_type_id": "6",
"content_id": "6",
"member_id": { "$ne": "51758" }
})
.sort({ "$natural": -1 })
.limit(10);
So what does that do? You match the various conditions in the data to do the "category match" (if I understood what was meant), then you make sure you are not matching entries by the same member.
The last parts do the "natural" sort. This is important because the ObjectId is monotinic, or math speak for "ever increasing". This means the "newest" entries are always the "highest" value. So descending order is "latest" to "oldest".
And the very final part is a basic "limit". So just return the last 10 entries.
As long as you can "filter" within the same collection in whatever way you want, then this should be fine.
I started using couchbase,
i like it a lot but one thing i cant find,
making a dynamic query,
{
"sender_name": "roman",
"sender_id": 123,
"content": "Hello World"
}
Now i want to query for document where "sender_id" = ?.
It can be any number,
Regular view with doc and meta cant help me because i dont know the value,
I should expect any sender_id.
Hope you can help me, thanks alot.
Ok , with Couchbase you can write Map&Reduce functions, and create views. The map functions accept parameters. I am not quite familiar with writing a Map function, but from couchbase.com I think this map function will do your job.
function(doc, meta)
{
emit(doc.sender_id, [doc.content]);
}
And your query would be ?key=["123"]
Go through these links
http://hardlifeofapo.com/creating-an-e-commerce-platform-using-couchbase-2/
http://hardlifeofapo.com/basic-couchbase-querying-for-sql-people/
http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-sql-where.html
here's my model:
class Person
acts_as_tree
end
i relate multiple objects as a tree:
P1
|
---
| |
P1.1 P1.2
|
---
| |
P1.1.1 P1.1.2
here if i need to retreive P1.1.1 i need to write a query that effectively asks:
get me the Person with name P1.1.1 and path (given by acts_as_tree) [P1, P1.1].
querying by just name is not enough as i can have similar named people at multiple paths.
how do i do this?
> db.people.find({name: 'P1.1.1'})
above snippet will show me the path attribute correctly as expected, but i cannot query by that path.
> db.people.find({name: 'P1.1.1', path: [{name: 'P1'}, {name: 'P1.1'}]})
doesn't work. neither does:
> db.people.find({name: 'P1.1.1', path: [db.people.find({name: 'P1'}),
db.people.find({name: 'P1.1'})]})
but that explains what i'm trying to do.
One of the ways where you can query something like as follows:
db.people.find({name : 'Joe', 'path' : { $all : [ObjectId("4e0fcf1722b7a9439200002e"), ObjectId("4e0fcf1622b7a9439200002b")]}})
However the drawback I think of this is:
You don't get to substitute the object relations/joins directly in mongo shell. You have to use the ObjectId object
The $all clause does not necessitate that the order of path is strictly same, which means that a person with name "Joe" referenced by path "hometown/town/" would come up as well as "Joe" from "tome/hometown".
I would presume that second one may be a deal breaker. Also, I am presuming that mongoid in several cases pass across the query options directly to mongodb (or atleast there are ways to do that). Hence it should be possible to do a search in ruby code using given query above.
Nevertheless, I'll do some more re-search on this and post my findings back.
Hope it helps.
Edit
To alleviate the second problem above there is also another way to query a person with a specific path. Find it below:
db.people.find({name : 'Joe', 'path.0' :ObjectId("4e0fcf1722b7a9439200002e"), 'path.1':ObjectId("4e0fcf1622b7a9439200002b")})
This would ensure that path is exactly what you are looking for. However this works in mongodb shell and you may still need to figure out, how mongoid can run an equivalent of this. Plus you may have to construct this query dynamically to create a path for person and that (for deep nested people) may just become long and ugly.
I would suggest skimming through following links on mongodb documentation to get a better understanding.
http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray
I hope this is what you were looking for.