Context
On MongoDB Atlas, using the JSON view:
Question
I want to insert a document which has a datetime field. I think I need to use the ISO 8601 format. But I've no idea how it should be formated. The MongoDB documentation doesn't tell a lot about it. I want to do it like this because I'm using the output of a python script but is it even possible?
Following is working:
db.test.insert({ _id:ObjectId("6027dd515ce9cb94d55744b0") , created_at:ISODate("2021-02-13T15:02:21.565762") })
WriteResult({ "nInserted" : 1 })
db.test.find()
{ "_id" : ObjectId("6027dd515ce9cb94d55744b0"), "created_at" : ISODate("2021-02-13T15:02:21.566Z") }
It can be done like this:
{
"_id": {
"$oid": "60280eb44567b5fc13d18d1e"
},
"created_at": {
"$date": {
"$numberLong": "1613224941565"
}
}
}
Related
My data in MongoDB:
{
"_id" : NumberLong(411694285),
"transactionDataList" : [
"{\"key\":\"6b8020acd2fdf393e67459dcb5d241cd2\",\"txaction\":\"appoint\",\"portalid\":\"2027601\",\"aid\":\"2344\",\"clearingtype\":\"sb\",\"notify_version\":\"7.4\",\"txtime\":\"32414\",\"currency\":\"EUR\",\"userid\":\"2324035\",\"accessname\":\"\",\"accesscode\":\"\",\"param\":\"cohort-496666-0-0-12\",\"mode\":\"live\",\"price\":\"169.95\",\"id\":{\"1\":\"Tei\"},\"pr\":{\"1\":\"169.95\"},\"no\":{\"1\":\"1\"},\"de\":{\"1\":\"Te 12 Monate\"},\"ti\":{\"1\":\"Te12 Monate\"},\"va\":{\"1\":\"19.00\"},\"txid\":\"4132432285\",\"reference\":\"4906-65a84dsdf3\",\"sequencenumber\":\"1\",\"company\":\"\",\"firstname\":\"Max\",\"lastname\":\"Mustermann\",\"street\":\"Weg 37\",\"zip\":\"33333\",\"city\":\"Moll",\"email\":\"muster#gmail.de\",\"country\":\"DE\",\"customerid\":\"2342\",\"balance\":\"0\",\"receivable\":\"111.95\"}"
],
"_class" : "de.xxxx.creatinginvoice.model.TransactionModel"
}
I want to filter my data in MongoDB. Like:
db.getCollection('transaction').find({"transactionDataList":{$elemMatch: { "lastname":"Mustermann" } }});
But I got always no data after filtering. Don't know, why.
Try this-
db.collection.aggregate({
"$match": {
"transactionDataList.lastname": "Mustermann"
}
})
Mongo Playground
i am pretty new to mongodb. i have a collection or json file like below.
{
"id": {
"timestamp": 1592538583,
"machineIdentifier": 1772242,
"processIdentifier": -7129,
"counter": 2887223,
"timeSecond": 1592538583,
"time": 1592538583000,
"date": 1592538583000
},
"creationTimestamp": 1592538583524,
"lastUpdateTimestamp": 1592538642832,
"idAsString": "5eec35d71b0ad2e4272c0e37"
}
i need to extract records based on timestamp. when i give below format its working. But i need human readable format. like lastUpdateTimestamp greater than "2020-06-30T00:00:00Z". i tried many waysbut getting bson format errors. any suggestions?
{ "lastUpdateTimestamp": { $gt : new Date(1592282308044) }}
Try to use it as a number.
{ "lastUpdateTimestamp": { $gt : 1592282308044 }}
Is it possible to aggregate on data that is stored via DBRef?
Mongo 2.6
Let's say I have transaction data like:
{
_id : ObjectId(...),
user : DBRef("user", ObjectId(...)),
product : DBRef("product", ObjectId(...)),
source : DBRef("website", ObjectId(...)),
quantity : 3,
price : 40.95,
total_price : 122.85,
sold_at : ISODate("2015-07-08T09:09:40.262-0700")
}
The trick is "source" is polymorphic in nature - it could be different $ref values such as "webpage", "call_center", etc that also have different ObjectIds. For example DBRef("webpage", ObjectId("1")) and DBRef("webpage",ObjectId("2")) would be two different webpages where a transaction originated.
I would like to ultimately aggregate by source over a period of time (like a month):
db.coll.aggregate( { $match : { sold_at : { $gte : start, $lt : end } } },
{ $project : { source : 1, total_price : 1 } },
{ $group : {
_id : { "source.$ref" : "$source.$ref" },
count : { $sum : $total_price }
} } );
The trick is you get a path error trying to use a variable starting with $ either by trying to group by it or by trying to transform using expressions via project.
Any way to do this? Actually trying to push this data via aggregation to a subcollection to operate on it there. Trying to avoid a large cursor operation over millions of records to transform the data so I can aggregate it.
Mongo 4. Solved this issue in the following way:
Having this structure:
{
"_id" : LUUID("144e690f-9613-897c-9eab-913933bed9a7"),
"owner" : {
"$ref" : "person",
"$id" : NumberLong(10)
},
...
...
}
I needed to use "owner.$id" field. But because of "$" in the name of field, I was unable to use aggregation.
I transformed "owner.$id" -> "owner" using following snippet:
db.activities.find({}).aggregate([
{
$addFields: {
"owner": {
$arrayElemAt: [{ $objectToArray: "$owner" }, 1]
}
}
},
{
$addFields: {
"owner": "$owner.v"
}
},
{"$group" : {_id:"$owner", count:{$sum:1}}},
{$sort:{"count":-1}}
])
Detailed explanations here - https://dev.to/saurabh73/mongodb-using-aggregation-pipeline-to-extract-dbref-using-lookup-operator-4ekl
You cannot use DBRef values with the aggregation framework. Instead you need to use JavasScript processing of mapReduce in order to access the property naming that they use:
db.coll.mapReduce(
function() {
emit( this.source.$ref, this["total_price"] )
},
function(key,values) {
return Array.sum( values );
},
{
"query": { "sold_at": { "$gte": start, "$lt": end } },
"out": { "inline": 1 }
}
)
You really should not be using DBRef at all. The usage is basically deprecated now and if you feel you need some external referencing then you should be "manually referencing" this with your own code or implemented by some other library, with which you can do so in a much more supported way.
I am trying to update a row in Mongo DB .
I have a collection named users
db.users.find()
{ "_id" : ObjectId("50e2efe968caee13be412413"), "username" : "sss", "age" : 32 }
I am trying to update the row with the username as Erinamodobo and modify the age to 55
I have tried the below way , but its not working .
db.users.update({ "_id": "50e2efe968caee13be412413" }, { $set: { "username": "Erinamodobo" } });
Please let me know where i am making the mistake ??
Pass in the _id as an ObjectId if you're using the mongo shell, otherwise, it won't find the existing user.
db.users.update({"_id": ObjectId("50e2efe968caee13be412413")},
{ "$set" :
{ "username": "Erinamodobo", "age" : "55" }})
With this query you are updating the name itself.
Notice that the syntax of an update is the following:
db.COLLECTION.update( {query}, {update}, {options} )
where query selects the record to update and update specify the value of the field to update.
So, in your case the correct command is:
db.users.update({ "name": "Erinamodobo" }, { $set: { "age": 55 } });
However, I suggets you to read the mongodb documentation, is very well written (http://docs.mongodb.org/manual/applications/update/)
As an extension to #WiredPrairie, even though he states the right answer he doesn't really explain.
The OjbectId is not a string, it is actually a Object so to search by that Object you must supply an Ojbect of the same type, i.e. here an ObjectId:
db.users.update({ "_id": ObjectId("50e2efe968caee13be412413") }, { $set: { "username": "Erinamodobo" } });
The same goes for any specific BSON type you use from Date to NumberLong you will need to wrap the parameters in Objects of the type.
In MongoDB, using $type, it is possible to filter a search based on if the field matches a BSON data type (see DOCS).
For eg.
db.posts.find({date2: {$type: 9}}, {date2: 1})
which returns:
{
"_id" : ObjectId("4c0ec11e8fd2e65c0b010000"),
"date2" : "Fri Jul 09 2010 08:25:26 GMT"
}
I need a query that will tell me what the actual type of the field is, for every field in a collection. Is this possible with MongoDB?
Starting from MongoDB 3.4, you can use the $type aggregation operator to return a field's type.
db.posts.aggregate(
[
{ "$project": { "fieldType": { "$type": "$date2" } } }
]
)
which yields:
{
"_id" : ObjectId("4c0ec11e8fd2e65c0b010000"),
"fieldType" : "string"
}
type the below query in mongo shell
typeof db.employee.findOne().first_name
Syntax
typeof db.collection_name.findOne().field_name
OK, here are some related questions that may help:
Get all field names in a collection using map-reduce.
Here's a recursive version that lists all possible fields.
Hopefully that can get you started. However, I suspect that you're going to run into some issues with this request. There are two problems here:
I can't find a "gettype" function for JSON. You can query by $type, but it doesn't look like you can actually run a gettype function on a field and have that maps back to the BSON type.
A field can contain data of multiple types, so you'll need a plan to handle this. Even if it's not apparent Mongo could store some numbers as ints and others floats without you really knowing. In fact, with the PHP driver, this is quite possible.
So if you assume that you can solve problem #1, then you should be able to solve problem #2 using a slight variation on "Get all field Names".
It would probably look something like this:
"map" : function() { for (var key in this) { emit(key, [ typeof value[key] ]); } }
"reduce" : function(key, stuff) { return (key, add_to_set(stuff) ); }
So basically you would emit the key and the type of key value (as an array) in the map function. Then from the reduce function you would add unique entries for each type.
At the end of the run you would have data like this
{"_id":[255], "name" : [1,5,8], ... }
Of course, this is all a lot of work, depending on your actual problem, you may just want to ensure (from your code) that you're always putting in the right type of data. Finding the type of data after the data is in the DB is definitely a pain.
Taking advantage of the styvane query, I added a $group listing to make it easier to read when we have different data types.
db.posts.aggregate(
[
{ "$project": { _id:0, "fieldType": { "$type": "$date2" } } },
{"$group": { _id: {"fieldType": "$fieldType"},count: {$sum: 1}}}
])
And have this result:
{ "_id" : { "fieldType" : "missing" }, "count" : 50 }
{ "_id" : { "fieldType" : "date" }, "count" : 70 }
{ "_id" : { "fieldType" : "string" }, "count" : 10 }
Noting that a=5;a.constructor.toString() prints function Number() { [native code] }, one can do something similar to:
db.collection.mapReduce(
function() {
emit(this._id.constructor.toString()
.replace(/^function (\S+).+$/, "$1"), 1);
},
function(k, v) {
return Array.sum(v);
},
{
out: { inline: 1 }
});