I have a schema, with a document structure that looks like this:
{ "_id" : ObjectId( "4f8dcb06ee21783d7400003c" ),
"venue" : ObjectId( "4f8dcb06ee21783d7400003b" ),
"name" : "Some event",
"webite: "www.whatever.com",
"attendees" : [
{ "_id" : ObjectId( "4f8dfb06ee21783d7134503a" ), "firstName" : "Joe", "lastName" : "Blogs", "emailAddress" : "some#thing1.com" },
{ "_id" : ObjectId( "4f8dfb06ee21783d7134503b" ), "firstName" : "John", "lastName" : "West", "emailAddress" : "some#thing2.com" }
{ "_id" : ObjectId( "4f8dfb06ee21783d7134503c" ), "firstName" : "Simon", "lastName" : "Green", "emailAddress" : "some#thing3.com" }
{ "_id" : ObjectId( "4f8dfb06ee21783d7134503d" ), "firstName" : "Harry", "lastName" : "Smith", "emailAddress" : "some#thing4.com" }
],
"eventType" : "Party"
}
I have a CSV file, that I'd like to import into the attendees collection...
Is that possible using mongoimport?
I read this: Using mongoimport to read CSV into nested structure? and it seems as though the answer could be no...
Since this is a one time operation, I wouldn't mind if I had to import it into an "attendees" collection, then run another command to insert into the attendees collection within my document? (there's only one event document at the moment....)
You are correct, and unfortunately the answer is no.
Mongoimport is a very simple program. (You can see for yourself here: https://github.com/mongodb/mongo/blob/master/src/mongo/tools/import.cpp) For users who would like to import data that is more complicated than a 2-dimensional table of strings and numbers, the official advice is to write a custom script that will read in your input file and create documents in exactly the format that you would like them. Hopefully the import.cpp file will give you some ideas on how to get started.
This maybe very late, but will be useful for future users running mongoDB > 4.x
You can have sub-document imported using a csv file by using a dot operator on the name.
for eg. if you want a document with a sub document called extra containing taxes, state & created_at, then you can name your fields as extra.taxes.auto(), extra.state.auto(), and extra.created_at.date(2006-01-02).
I was stuck on this question and couldn't find help online so posting it here.
Hope this helps someone.
Related
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.
I have a MongoDB collection called Users.
I do not know object types in advance.
This collection has at least 3 different type structured of objects. For example:
Type 1:
{
"_id" : "9e1736d4-f3a1-47ed-bb51-3318129664f0",
"userid" : 6711,
"registerDate" : "2014-10-28T14:42:06",
"lastLoginDate" : "2014-10-28T14:42:06",
}
Type 2:
{
"_id" : "9e1736d4-f3a1-47ed-bb51-3318129664f1",
"userid" : 6712,
"email" : "johndoe#example.com",
"username" : "john doe",
}
Type 3:
{
"_id" : "9e1736d4-f3a1-47ed-bb51-3318129664f2",
"userid" : 63713,
"city" : "orange",
"state" : "new york",
"country" : "US",
}
How can I get distinct types (or top 1st object from each type) from my collection?
So if I have 1 million users and 3 different structure above, I would like to get 3 results.
Add a field "type" identifing type of the document.
Then use db.collection.group to group results on your wish.
Issue query for each type, you may
db.c.findOne({registerDate: {$existed: true}});
db.c.findOne({email: {$existed: true}});
db.c.findOne({city: {$existed: true}});
This should be very fast and simple. :)
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
I have two collection as bellow products has reference of user. i search product by name & in return i want combine output of product and user using map reduce method
user collection
{
"_id" : ObjectId("52ac5dd1fb670c2007000000"),
"company" : {
"about" : "This is textile machinery dealer",
"contactAddress" : [{
"address" : "abcd",
"city" : "52ac4bc6fb670c1007000000",
"zipcode" : "39as46as80"
},{
"address" : "abcd",
"city" : "52ac4bc6fb670c1007000000",
"zipcode" : "39as46as80"
}],
"fax" : "58784868",
"mainProducts" : "ads,asd,asd",
"mobileNumber" : "9537236588",
"name" : "krishna steels",
}
"user" : ObjectId("52ac4eb7fb670c0c07000000")
}
product colletion
{
"_id" : ObjectId("52ac5722fb670cf806000002"),
"category" : "52a2a9cc48a508b80e00001d",
"deliveryTime" : "10 days after received the ",
"price" : {
"minPrice" : "2000",
"maxPrice" : "3000",
"perUnit" : "5288ac6f7c104203e0976851",
"currency" : "INR"
},
"productName" : "New Mobile Solar Charger with Carabiner",
"rejectReason" : "",
"status" : 1,
"user" : ObjectId("52ac4eb7fb670c0c07000000")
}
This cannot be done. Mongo support Map Reduce only on one collection. You could try to fetch and merge in a java collection. Couple of days back I solved a similar problem using java collection.
Click to see similar response about joins and multi collection not supported in mongo.
This can be done using two map reduces.
You run your first MR and then you reduce out the second MR onto the results of the first.
You shouldn't do this though. JOINs are not designed to be done through MR, in fact it sounds like you are trying to do this MR with inline output which in itself is a very bad idea.
MRs are not designed to run inline to the application.
You would be better off doing the JOIN else where.
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)