mongo export array elements - mongodb

I would like to export 3 different csv files, here is my document
{
"capacities" : [
{
"size" : "A",
"incoming_parcels" : 27,
"outgoing_parcels" : 0,
"empty_compartments" : 0
},
{
"size" : "B",
"incoming_parcels" : 11,
"outgoing_parcels" : 0,
"empty_compartments" : 8
},
{
"size" : "C",
"incoming_parcels" : 2,
"outgoing_parcels" : 1,
"empty_compartments" : 7
}
]
}
I would like to get all documents where capacities[1] = B and then get all fields - same for all sizes.
Here is my syntax for export :
mongoexport.exe --db name --collection name --type csv --out sizeB.csv -q "{'capacities.1.size': 'B'}" -f size,incoming_parcels,outgoing_parcels,empty_compartments
I've also tried -f capacities.1.size etc

One approach you could take is to use the aggregation framework to filter your documents using the above query as your $match operator and then write the documents returned by the aggregation pipeline to a specified collection using the $out operator. You can then export the data from that aggregation output collection. The following outlines the concept:
db.test.aggregate([
{
"$match": {
"capacities.size": "B"
}
},
{
"$unwind": "$capacities"
},
{
"$match": {
"capacities.size": "B"
}
},
{
"$project": {
"size" : "$capacities.size",
"incoming_parcels" : "$capacities.incoming_parcels",
"outgoing_parcels" : "$capacities.outgoing_parcels",
"empty_compartments" : "$capacities.empty_compartments",
}
},
{
"$out": "capacities_output"
}
])
Export to csv:
mongoexport.exe --db name --collection "capacities_output" --csv > sizeB.csv --fields size,incoming_parcels,outgoing_parcels,empty_compartments

Related

How to reduce a list of documents into a list of values in MongoDB

I ran an aggregation pipeline with a lookup stage on my database filtering out the data that I need and joining it with the data from another collection. I have, in the end, received something like this:
{
"_id" : ObjectId("5e53b804a72bb4185c682a00"),
"sample_ids" : [
{
"sample" : ObjectId("5e4fac16ad485744e34a799c")
},
{
"sample" : ObjectId("5e4fac18eaf0df39564a799b")
},
{
"sample" : ObjectId("5e4fac19ad485744e34a799e")
},
{
"sample" : ObjectId("5e4fac16eaf0df39564a799a")
}
]
}
I'm wondering whether it is possible to "squeeze" or "reduce" the list of documents to a single list of values so that I receive something like this instead:
{
"_id" : ObjectId("5e53b804a72bb4185c682a00"),
"sample_ids" : [
ObjectId("5e4fac16ad485744e34a799c"),
ObjectId("5e4fac18eaf0df39564a799b"),
ObjectId("5e4fac19ad485744e34a799e"),
ObjectId("5e4fac16eaf0df39564a799a")
]
}```
Simply like this:
db.collection.aggregate([
{
$project: {
sample_ids: "$sample_ids.sample"
}
}
])

Changing strings in mongodb collection to uppercase

I've searched a lot but still can't find (or understand) the answer. I have a collection in my mongodb database that is called "btest". Inside of that collection I have a list of randomly generated strings (from 1000 to 1000000) that look something like this:
> db.btest.find()
{ "_id" : ObjectId("5818ed42c33b12a7c902cd34"), "0" : 1, "wgickjkwxfimleot" : "r
scjuvarvmvuheom" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd35"), "0" : 2, "wgickjkwxfimleot" : "t
gdqnegjscsmnjsi" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd36"), "0" : 4, "wgickjkwxfimleot" : "d
qjvndthelmtqknj" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd37"), "0" : 5, "wgickjkwxfimleot" : "u
qtmbuhgwxntcixh" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd38"), "0" : 6, "wgickjkwxfimleot" : "i
rguwjvectjvimjk" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd39"), "0" : 7, "wgickjkwxfimleot" : "n
sggjpodfvebjumk" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd3a"), "0" : 8, "wgickjkwxfimleot" : "a
wvjtlxtoqwpdltp" }
I imported these using this command:
mongoimport -d ee_db -c btest --type csv --file "C:\Users\USER\Desktop\Random\projekty java\ee_bulk_insert\src\10000.csv" --headerline
What I want to do is to change all the strings in this collection to uppercase letters. In MySQL I've done the same thing with the "SELECT UCASE(row) FROM btest;" command.
How to achieve the same result in MongoDB? Thanks for all the answers.
You can use an aggregation with a $project using $toUpper to convert string to uppercase and then write the results in a new collection with $out :
db.btest.aggregate(
[{
$project: {
"0": 1,
"wgickjkwxfimleot": { $toUpper: "$wgickjkwxfimleot" }
}
}, {
$out: "results"
}]
)

MongoDB: updating the value of an inner BSON

I have a document with a schema in mongodb that looks like this:
{
"_id" : ObjectId("572f88424de8c74a69d4558c"),
"storecode" : "ABC",
"credit" : true,
"group" : [
{
"group_name" : "Frequent_Buyer",
"time" : NumberLong("1462732865712"),
}
],
}
I want to add on the _id part for the first object in the array under group so it looks like this:
{
"_id" : ObjectId("572f88424de8c74a69d4558c"),
"storecode" : "ABC",
"credit" : true,
"group" : [
{
"group_name" : "Frequent_Buyer",
"time" : NumberLong("1462732865712"),
"_id" : "573216fee4430577cf35e885"
}
],
}
When I try this code it fails:
db.customer.update({ "_id": ObjectId("572f88424de8c74a69d4558c") },{ "$set": { "group.$._id": "573216fee4430577cf35e885"} })
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: groups.$._id"
}
})
However, if I adjust the code slightly and add on an extra criteria for querying, it works:
db.customer.update({ "_id": ObjectId("572f88424de8c74a69d4558c"), "group.groupname": "Frequent_Buyer" },{ "$set": { "group.$._id": "573216fee4430577cf35e885"} })
Results:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Why did the first command not work but the second command work?
This is the expected result. To use the positional $ update operator, the array field must appear as part of the query document as mentioned in the documentation.
When used with update operations, e.g. db.collection.update() and db.collection.findAndModify(),
the positional $ operator acts as a placeholder for the first element that matches the query document, and
the array field must appear as part of the query document.
For example If you don't want to filter your documents using group_name, simply add group: { "$exists": true } or "group.0": { "$exists": true } to your query criteria. You query will then look like this:
db.customer.updateOne(
{
"_id": ObjectId("572f88424de8c74a69d4558c"),
"group.0": { "$exists": true}
},
{ "$set": { "group.$._id": "573216fee4430577cf35e885" } }
)
Last and not least, you should be using updateOne or updateMany because update is deprecated in official language driver.

How to Avoid Duplicate Entries in MongoDb Meteor App

How to avoid duplicate entries in mongoDb in Meteor application.
On the command: db.products.find({},{"TEMPLATE_NAME": 1},{unique : true})
{ "_id" : ObjectId("5555d0a16ce3b01bb759a771"), "TEMPLATE_NAME" : "B" }
{ "_id" : ObjectId("5555d0b46ce3b01bb759a772"), "TEMPLATE_NAME" : "A" }
{ "_id" : ObjectId("5555d0c86ce3b01bb759a773"), "TEMPLATE_NAME" : "C" }
{ "_id" : ObjectId("5555d0f86ce3b01bb759a774"), "TEMPLATE_NAME" : "C" }
{ "_id" : ObjectId("5555d1026ce3b01bb759a775"), "TEMPLATE_NAME" : "A" }
{ "_id" : ObjectId("5555d1086ce3b01bb759a776"), "TEMPLATE_NAME" : "B" }
I want to retrieve only the unique template names and show them on HTML page.
Use the aggregation framework where your pipeline stages consist of the $group and $project operators respectively. The $group operator step groups the input documents by the given key and thus will return distinct documents in the result. The $project operator then reshapes each document in the stream, such as by adding new fields or removing existing fields:
db.products.aggregate([
{
"$group": {
"_id": "$TEMPLATE_NAME"
}
},
{
"$project": {
"_id": 0,
"TEMPLATE_NAME": "$_id"
}
}
])
Result:
/* 0 */
{
"result" : [
{
"TEMPLATE_NAME" : "C"
},
{
"TEMPLATE_NAME" : "A"
},
{
"TEMPLATE_NAME" : "B"
}
],
"ok" : 1
}
You could then use the meteorhacks:aggregate package to implement the aggregation in Meteor:
Add to your app with
meteor add meteorhacks:aggregate
Then simply use .aggregate function like below.
var products = new Mongo.Collection('products');
var pipeline = [
{
"$group": {
"_id": "$TEMPLATE_NAME"
}
},
{
"$project": {
"_id": 0,
"TEMPLATE_NAME": "$_id"
}
}
];
var result = products.aggregate(pipeline);
-- UPDATE --
An alternative that doesn't use aggregation is using underscore's methods to return distinct field values from the collection's find method as follows:
var distinctTemplateNames = _.uniq(Collection.find({}, {
sort: {"TEMPLATE_NAME": 1}, fields: {"TEMPLATE_NAME": true}
}).fetch().map(function(x) {
return x.TEMPLATE_NAME;
}), true)
;
This will return an array with distinct product template names ["A", "B", "C"]
You can check out some tutorials which explain the above approach in detail: Get unique values from a collection in Meteor and METEOR – DISTINCT MONGODB QUERY.
You can use distinct of mongodb like :
db.collectionName.distinct("TEMPLATE_NAME")
This query will return you array of distinct TEMPLATE_NAME

MongoDB GeoNear Aggregate

The question is:
Consider the following location: [-72, 42] and the range (circle) of radius 2 around this point. Write a query to find all the states that intersect this range (circle). Then, you should return the total population and the number of cities for each of these states. Rank the states based on number of cities.
I have written this so far:
db.zips.find({loc: {$near: [-72, 42], $maxDistance: 2}})
and a sample output of that is:
{ "city" : "WOODSTOCK", "loc" : [ -72.004027, 41.960218 ], "pop" : 5698, "state" : "CT", "_id" : "06281" }
In SQL i would simply do a group by "state", how would i be able to do that here while also counting all the cities and total population?
assuming you follow the mongoimport routine for its zipcode data (i brought mine into a collection called zips7):
mongoimport --db mydb --collection zips7 --type json --file c:\users\drew\downloads\zips.json
or
mongoimport --db mydb --collection zips7 --type json --file /data/playdata/zips.json
(depending on your OS and paths)
then
db.zips7.ensureIndex({loc:"2d"})
db.zips7.find({loc: {$near: [-72, 42], $maxDistance: 2}}).forEach(function(doc){
db.zips8.insert(doc);
});
note that db.zips7.stats() shows like 30k rows and zips8 has 100 rows
db.zips8.aggregate( { $group :
{ _id : "$state",
totalPop : { $sum : "$pop" },
town_count:{$sum:1}
}}
)
{
"result" : [
{
"_id" : "RI",
"totalPop" : 39102,
"town_count" : 10
},
{
"_id" : "MA",
"totalPop" : 469583,
"town_count" : 56
},
{
"_id" : "CT",
"totalPop" : 182617,
"town_count" : 34
}
],
"ok" : 1
}
Syntax in mongoid
Zips.where(:loc => {"$within" => {"$centerSphere"=> [[lng.to_f,lat.to_f],miles.to_f/3959]}})
Example:
Zips.where(:loc => {"$within" => {"$centerSphere"=> [[-122.4198185,37.7750454],2.0/3959]}})