NumberLong() in MongoDB && PHP - php

db.projects.find()
{
"_id" : ObjectId("508d4028e60d8c2154a2eb36"),
"date" : NumberLong(1351432927),
"deadline" : "2012-12-31",
"payment" : "100500",
"title" : "Project",
"userid" : ObjectId("5083677d2f5c849509c3aae6")
}
All right, but in PHP print_r($find_result) doesn't show the key "date" at all!
Why? And how I can extract value from NumberLong() in PHP?

Related

ISODate format in mongodb version

I had restored MongoDB server version: 4.2.3 to MongoDB server version: 4.2.7 and I had an error about ISODate as below when saving data to the database again:
{ "_id" : ObjectId("5ed4b193ed6fab6d2272c5c4"), "id" : 1, "timestamp" : ISODate("2020-05-31T05:59:59Z") } #new data run after change db (it must disappear for unique)
{ "_id" : ObjectId("5ed33bef1e499012bf35e412"), "id" : 1, "timestamp" : ISODate("2020-05-31T04:59:59.999Z") } #old data
{ "_id" : ObjectId("5ed4b193ed6fab6d2272c5c3"), "id" : 1, "timestamp" : ISODate("2020-05-31T04:59:59Z") } #new data run after change db (it must disappear for unique)
{ "_id" : ObjectId("5ed32de165269b416f6c7362"), "id" : 1, "timestamp" : ISODate("2020-05-31T03:59:59.999Z") } #old data
{ "_id" : ObjectId("5ed4b193ed6fab6d2272c5c2"), "id" : 1, "timestamp" : ISODate("2020-05-31T03:59:59Z") } #new data run after change db (it must disappear for unique)
{ "_id" : ObjectId("5ed31fcff2a5076cc947bc02"), "id" : 1, "timestamp" : ISODate("2020-05-31T02:59:59.999Z") } #old data
{ "_id" : ObjectId("5ed311bfb0d88300f81e90d2"), "id" : 1, "timestamp" : ISODate("2020-05-31T01:59:59.999Z") } #old data
I have an index id and timestamp which is unique, but because timestamp has microseconds, not exactly so. Please give me a solution to keep microseconds in an ISODate.
PS: my code did not change. I use PHP and always format dates with 'Y-m-d\TH:i:s.uP'
MongoDB time resolution is 1 millisecond. Values with more precision will be truncated to millisecond precision.

add object id mongo db php document

{
"_id" : ObjectId("5844dd1d3627570f004612a5"),
"updatedBy" : ObjectId("57c4799d23be243006e188f8"),
"updatedAt" : ISODate("2016-12-21T10:08:10.211Z"),
"createdBy" : ObjectId("5801b7195248ef0e00948934"),
"createdAt" : ISODate("2016-12-05T03:21:01.660Z"),
"stock" : 50,
"price" : 15000,
"owner" : ObjectId("5801b7195248ef0e00948934"),
"type" : ObjectId("57dce9ad07f96c701c0b24a8"),
"isValid" : true,
"grade" : "Grade A",
"__v" : 0
}
i want to ask how to insert owner data and also type data as ObjectID to mongoDB with php
when i just insert data, it wont changes to object id
anyone?
i got the answer.
just use like this :
$id_param = 1270982704y1024kjh12kj4h; new MongoId($id_param)
thanks a lot your for help

Intelligent searching with mongo

I am using phalcon with mongodb. I have the following document in collection:
{
"_id" : ObjectId("547c8b6f7d30dd522b522255"),
"title" : "Test vacancy",
"slug" : "test-vacancy",
"location" : "the-netherlands",
"contract" : "fixed",
"function" : "Test vacancy",
"short_description" : "gdfsgfds",
"description" : "fdsafsdgfsdgdfa",
"promo_text" : "gfdsgdfs",
"company_name" : "gfdsgfsd",
"hits" : 36,
"updated_at" : 1.42685e+09,
}
In controller I am fetching all results by searched phase/query. For example I put example word and output will be all posts with example word in description or title or short_desc etc. Everything is correct but I want sort these posts in specific order. I mean if query will be same as title, this post should be first. Now it is somewhere below.
Can you help me? Thank you in advance.

Mongo and Yii -> update with $set a field in all the arrays of a subdocument

I'm having problems updating a specific field in all the arrays of a subdocument. I have the following structure in MongoDB:
{
"_id" : ObjectId("539c9e97cac5852a1b880397"),
"DocumentoDesgloseER" : [
{
"elemento" : "COSTO VENTA",
"id_rubroer" : "11",
"id_documento" : "45087",
"abreviatura" : "CV",
"orden" : "1",
"formula" : "Cuenta Contable",
"tipo_fila" : "1",
"color" : "#FFD2E9",
"sucursal" : "D",
"documentoID" : "0",
"TOTAL" : "55426.62",
},
{ ... MORE OF THE SAME ... }
],
"id_division" : "2",
"id_empresa" : "9",
"id_sucursal" : "37",
"ejercicio" : "2008",
"lastMonthNumber" : NumberLong(6),
}
I need to update the field "documentoID" to a specific value; like "20" for example, in all the arrays of the subdocument "DocumentoDesgloseER". How I can do this?
I tried the following (with $ operator) and is not working:
$querySearch = array('id_division'=>'2', 'id_empresa'=>'9', 'id_sucursal'=>'37', 'ejercicio'=>'2008');
$queryUpdate = array('$set'=>array('DocumentoDesgloseER.$.documentoID'=>'20'));
Yii::app()->edmsMongoCollection('DocumentosDesgloseER')->update($querySearch,$queryUpdate);
By the way, I'm using Yii Framework to make the connection with Mongo. Any help or advice is welcome.
Thanks ;D!
Unfortunately, you can't currently use a positional operator to update all items in an array. There is a ticket opened in the MongoDB JIRA about this issue.
There a two "solutions":
Change your schema so that your embedded documents are in the separate collection (it's probably not what you want).
The best you can do, if you don't want to change your schema, is to update each subdocument in PHP and then save the whole document.

MongoDB Find All Comments

If I have several documents that use the following structure:
{
"_id" : ObjectId("4f9c6fe033cddec1bea430f2"),
"title" : "Product #1",
"comments" :
[
{
"comment" : "My comment",
"date" : "2012-03-01 00: 00: 00",
"name" : "Test Name",
"username" : "Test Username"
},
{
"comment" : "My comment",
"date" : "2012-03-01 00: 00: 00",
"name" : "Test Name 2",
"username" : "Test Username 2"
}
]
}
Is it possible for me to find all documents that have reviews.name and update it? For example say I want to find all documents with user.name : test name 2 and change it to Cassie. Is that possible? If not would it be better to put the comments in a different collection?
Yes it is possible. You would have to use comments.username to find it and then comments.$.username note that since you are posting to an array you have to tell mongo to go in there using the $ positional operator

Categories