Problem with timestamp formats in .find() in nodejs/express/Mongoose js - javascript

I have a collection like this;
{
"_id" : ObjectId("5d428c8b0edc602c155929c5"),
"source" : "connection",
"class" : "dns",
"ts" : 1503528301.909886,
"uid" : "C8avTr2cLyrJJxkN9",
}
If I print the key and type of key in mongo shell, I can see that ts actually is a string:
ts string
When I receive a query, I need to get the ts value from the URL and do a .find() query.
GET http://localhost:3300/alerts?ts=1503528332.909886&limit=100&page=1
startTs = req.query.ts
This may have to be converted to a ts without the '.' after the second. I have looked at converting 1503528332.909886 to float, multiply by 1000 1503528332909.886 and truncate to integer 1503528332909. I have also tried using both string and number format.
results.results = await model
.find({"ts": {"$gte": <ts_variable: what format do I use?> }})
.skip(startIndex)
.exec();
res.paginatedResults = results;
If I only use ".find({})" and not try to select based on ts, everything works as expected. Appreciate any tips you have.

Related

How to replace Schema «availability» string with multiple values?

I am noob with structured data implementation and don't have any code knowledge. I have been looking for a week how to solve my problem with "availability" in Google structured data testing tool.
My stocks on the front have 3 possibilities :
"Stock : 1234" (text + stock numbers)
"Stock : Coming soon"
"Stock : On demand"
Google Results example
But he problem is Goggle accept only in my case "InStock" or "OutOfStock"
I have a CSS variable element #PdtXQStock named in a variable "Product-stock"
How can have :
if "availability" string contain numbers (not is number) ==> output "InStock"
if "availability" string is "Stock : Coming soon" ==> output "OutOfStock"
if "availability" string is "Stock : On demand" ==> output "InStock"
What is the proper Custom Js function from beginning to end?
function(){
var string = {{Product-stock}};
string = "https://schema.org/InStock";
string = string.replace(/Stock : Coming Soon/g, "OutOfStock");
string = string.replace(/Stock : On demand/g, "InStock");
return string.replace;
}

string to JSON parse , javascript runtime invalid character

I am trying to read the values of properties in this string, when i try to parse it , i get invalid character. Can you tell me whats wrong here
data = [{'title' : 'location 1','lat' : '29.769730','lng' : '-95.257181','desc' : 'Apartments',},{'title' : 'location 2','lat' : '29.852264','lng' : '-95.469999','desc' : 'location description',},];
var test = $.parseJSON(data)l
error - Unhandled exception at line 138, column 13 in http://localhost:17765/Loc/index
0x800a03f6 - JavaScript runtime error: Invalid character
In your code, data is not a string. It is an array. It does not need parsing (other than by the JavaScript compiler). Just discard $.parseJSON and work with the data.
data = [{'title' : 'location 1','lat' : '29.769730','lng' : '-95.257181','desc' : 'Apartments',},{'title' : 'location 2','lat' : '29.852264','lng' : '-95.469999','desc' : 'location description',},];
data.forEach(o => console.log(o.title));
It is being returned from an mvc5 controller method as a string
If your code does not accurately reflect the data you have and you do have a string, then it would need parsing.
The code you provided is, however, not valid JSON which:
Requires strings be quoted with " not '
Does not allow trailing , after the last item in an array.
You need to fix the server side code so it returns real JSON.
This will probably involve replacing some code which attempts to generate JSON by mashing together strings with some which uses a JSON aware library function (see this question).
Your JSON is invalid, try this:
var data = '[{"title" : "location 1","lat" : "29.769730","lng" : "-95.257181","desc" : "Apartments"},{"title" : "location 2","lat" : "29.852264","lng" : "-95.469999","desc" : "location description"}]';
var test = $.parseJSON(data);
validate your JSON here
[{"title" : "location 1","lat" : "29.769730","lng" : "-95.257181","desc" : "Apartments"},{"title" : "location 2","lat" : "29.852264","lng" : "-95.469999","desc" : "location description"}]

Mongodb parse string to Decimal/float

i'm new to mongoDB. I got my data as a string but i want to parse it to a decimal/ float.
I found made this code but it doesn't seem to work. This is my code, it replaces - for 00, * for "", and parses it a float. It gives no errors, but the parseFloat(doc).toFixed(2).
db.behuizingen.find().forEach(function(doc)
{
var price = doc.Prijs.replace('€', ''); // it may be vary for your other document
price = price.replace('-', '00');
price = price.replace('*', '');
doc.Prijs = Number(price);
parseFloat(doc).toFixed(2)
db.behuizingen.update({_id : doc._id} , doc;
})
Thanks in advance.
You did this wrong. Convert first and the Number() function of the shell has nothing to do with it. So replacing that line an continuing:
doc.Prijs = parseFloat(parseFloat(price).toFixed(2));
db.moederborden.update({_id : doc._id} , doc );
But also be beware. That usage of .update() where the second argument is there will "replace" the entire document with the contents of doc.
You may want to do this instead:
doc.Prijs = parseFloat(parseFloat(price).toFixed(2));
var update = { "$set": {} };
Object.keys(doc).each(function(key) {
update["$set"][key] = doc[key];
});
db.moederborden.update({_id : doc._id} , update );
Which uses the $set operator, so that any existing values that were not referenced in your doc object are not overwritten when writing to the database.
I had a similar issue where we had not had decimal serialisation enabled correctly and so I wanted to update documents that were already in our database to use the Decimal type instead of String. I used the following script:
db.MyCollection.find({ Price: { $type: "string" } }).forEach(function(doc) {
print('Updating price for ' + doc._id);
db.MyCollection.update(
{ _id: doc._id },
{ $set : { Price: NumberDecimal(doc.Price) } }
)
})
This only retrieves the documents that need updating by filtering on the field type, and then uses NumberDecimal to convert the string into a decimal.

Convert ObjectID (Mongodb) to String in JavaScript

I want to convert ObjectID (Mongodb) to String in JavaScript.
When I get a Object form MongoDB. it like as a object has: timestamp, second, inc, machine.
I can't convert to string.
Try this:
objectId.str
See the doc.
ObjectId() has the following attribute and methods:
[...]
str - Returns the hexadecimal string representation of the object.
in the shell
ObjectId("507f191e810c19729de860ea").str
in js using the native driver for node
objectId.toHexString()
Here is a working example of converting the ObjectId in to a string
> a=db.dfgfdgdfg.findOne()
{ "_id" : ObjectId("518cbb1389da79d3a25453f9"), "d" : 1 }
> a['_id']
ObjectId("518cbb1389da79d3a25453f9")
> a['_id'].toString // This line shows you what the prototype does
function () {
return "ObjectId(" + tojson(this.str) + ")";
}
> a['_id'].str // Access the property directly
518cbb1389da79d3a25453f9
> a['_id'].toString()
ObjectId("518cbb1389da79d3a25453f9") // Shows the object syntax in string form
> ""+a['_id']
518cbb1389da79d3a25453f9 // Gives the hex string
Did try various other functions like toHexString() with no success.
You can use $toString aggregation introduced in mongodb version 4.0 which converts the ObjectId to string
db.collection.aggregate([
{ "$project": {
"_id": { "$toString": "$your_objectId_field" }
}}
])
Use toString:
var stringId = objectId.toString()
Works with the latest Node MongoDB Native driver (v3.0+):
http://mongodb.github.io/node-mongodb-native/3.0/
Acturally, you can try this:
> a['_id']
ObjectId("518cbb1389da79d3a25453f9")
> a['_id'] + ''
"518cbb1389da79d3a25453f9"
ObjectId object + String will convert to String object.
If someone use in Meteorjs, can try:
In server: ObjectId(507f191e810c19729de860ea)._str.
In template: {{ collectionItem._id._str }}.
Assuming the OP wants to get the hexadecimal string value of the ObjectId, using Mongo 2.2 or above, the valueOf() method returns the representation of the object as a hexadecimal string. This is also achieved with the str property.
The link on anubiskong's post gives all the details, the danger here is to use a technique which has changed from older versions e.g. toString().
In Javascript, String() make it easy
const id = String(ObjectID)
this works, You have mongodb object: ObjectId(507f191e810c19729de860ea),
to get string value of _id, you just say
ObjectId(507f191e810c19729de860ea).valueOf();
In Js do simply: _id.toString()
For example:
const myMongoDbObjId = ObjectID('someId');
const strId = myMongoDbObjId.toString();
console.log(typeof strId); // string
You can use string formatting.
const stringId = `${objectId}`;
toString() method gives you hex String which is kind of ascii code but in base 16 number system.
Converts the id into a 24 character hex string for printing
For example in this system:
"a" -> 61
"b" -> 62
"c" -> 63
So if you pass "abc..." to get objectId you will get "616263...".
As a result if you want to get readable string(char string) from objectId you have to convert it(hexCode to char).
To do this I wrote an utility function hexStringToCharString()
function hexStringToCharString(hexString) {
const hexCodeArray = [];
for (let i = 0; i < hexString.length - 1; i += 2) {
hexCodeArray.push(hexString.slice(i, i + 2));
}
const decimalCodeArray = hexCodeArray.map((hex) => parseInt(hex, 16));
return String.fromCharCode(...decimalCodeArray);
}
and there is usage of the function
import { ObjectId } from "mongodb";
const myId = "user-0000001"; // must contains 12 character for "mongodb": 4.3.0
const myObjectId = new ObjectId(myId); // create ObjectId from string
console.log(myObjectId.toString()); // hex string >> 757365722d30303030303031
console.log(myObjectId.toHexString()); // hex string >> 757365722d30303030303031
const convertedFromToHexString = hexStringToCharString(
myObjectId.toHexString(),
);
const convertedFromToString = hexStringToCharString(myObjectId.toString());
console.log(`convertedFromToHexString:`, convertedFromToHexString);
//convertedFromToHexString: user-0000001
console.log(`convertedFromToString:`, convertedFromToString);
//convertedFromToHexString: user-0000001
And there is also TypeScript version of hexStringToCharString() function
function hexStringToCharString(hexString: string): string {
const hexCodeArray: string[] = [];
for (let i = 0; i < hexString.length - 1; i += 2) {
hexCodeArray.push(hexString.slice(i, i + 2));
}
const decimalCodeArray: number[] = hexCodeArray.map((hex) =>
parseInt(hex, 16),
);
return String.fromCharCode(...decimalCodeArray);
}
Just use this : _id.$oid
And you get the ObjectId string. This come with the object.
Found this really funny but it worked for me:
db.my_collection.find({}).forEach((elm)=>{
let value = new String(elm.USERid);//gets the string version of the ObjectId which in turn changes the datatype to a string.
let result = value.split("(")[1].split(")")[0].replace(/^"(.*)"$/, '$1');//this removes the objectid completely and the quote
delete elm["USERid"]
elm.USERid = result
db.my_collection.save(elm)
})
On aggregation use $addFields
$addFields: {
convertedZipCode: { $toString: "$zipcode" }
}
Documentation of v4 (right now it's latest version) MongoDB NodeJS Driver says: Method toHexString() of ObjectId returns the ObjectId id as a 24 character hex string representation.
In Mongoose, you can use toString() method on ObjectId to get a 24-character hexadecimal string.
Mongoose documentation
Below three methods can be used to get the string version of id.
(Here newUser is an object containing the data to be stored in the mongodb document)
newUser.save((err, result) => {
if (err) console.log(err)
else {
console.log(result._id.toString()) //Output - 23f89k46546546453bf91
console.log(String(result._id)) //Output - 23f89k46546546453bf91
console.log(result._id+"") //Output - 23f89k46546546453bf91
}
});
Use this simple trick, your-object.$id
I am getting an array of mongo Ids, here is what I did.
jquery:
...
success: function (res) {
console.log('without json res',res);
//without json res {"success":true,"message":" Record updated.","content":[{"$id":"58f47254b06b24004338ffba"},{"$id":"58f47254b06b24004338ffbb"}],"dbResponse":"ok"}
var obj = $.parseJSON(res);
if(obj.content !==null){
$.each(obj.content, function(i,v){
console.log('Id==>', v.$id);
});
}
...
You could use String
String(a['_id'])
If you're using Mongoose along with MongoDB, it has a built-in method for getting the string value of the ObjectID. I used it successfully to do an if statement that used === to compare strings.
From the documentation:
Mongoose assigns each of your schemas an id virtual getter by default which returns the document's _id field cast to a string, or in the case of ObjectIds, its hexString. If you don't want an id getter added to your schema, you may disable it by passing this option at schema construction time.

JSON Serialization error with simplejson

I have the following code:
data = {'services': [u'iTunes'],
'orders': [u'TestOrder', u'Test_April_Titles_iTunes'],
'providers': ''}
return HttpResponse(simplejson.dumps(data))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in default
178. raise TypeError(repr(o) + " is not JSON serializable")
Exception Type: TypeError at /reports/change_dropdown/
Exception Value: [u'iTunes'] is not JSON serializable
What do I need to do to serialize this dictionary with a list inside it?
The problem is that itunes is a non-JSON compatible type.
To solve provide default type to convert non-JSON compatible types when serializing:
simplejson.dumps(data, default=str))
or even:
def handler(val):
if isinstance(val, unicode)
return str(val)
else:
return val
simplejson.dumps(data, default=handler))
The advantage of the second option is you can handle sets (e.g., convert to list), dates (e.g., convert to int timetstamp), etc.
Converting from unicode to str worked here:
data['services'] = [str(item) for item in data['services']]
data['orders'] = [str(item) for item in data['orders']]
data['providers'] = [str(item) for item in data['providers']]

Categories

Resources