내 mongodb 데이터베이스에 2 개의 콜렉션이 있습니다 : users
및 posts
. 그리고 posts
컬렉션이 같은 DBRef 필드가 있습니다
user : DBRef('users', ObjectId('...'), null)
, 나는 다음을 수행하십시오
db.posts.remove({ 'user.$id' : ObjectId('...') })
그리고 그것은 잘 작동을하지만, node-mongodb-native가 아닙니다. node-mongodb-native에서이 요청을하는 동안 다음 오류가 발생합니다.
key must not contain '.'
누구나 확인할 수 있습니까? 내가 뭔가 잘못 들었다면 당신의 도움과 설명에 감사드립니다.
업데이트 DBRef $ 아이디 작업 벌금
찾기 요청!
Node.js를 코드 :
var mongodb = require('mongodb')
, nconf = require('nconf')
, MongoClient = mongodb.MongoClient;
MongoClient.connect(nconf.get('db:connectionString'), function(mongoConnectionError, db) {
if (mongoConnectionError) throw mongoConnectionError;
db
.collection('posts')
.remove({ 'user.$id' : new mongodb.ObjectID('...') }, {}, function(err, removedItems) {
if (err) { throw err; }
console.log('Removed items: ' + removedItems);
});
});
질문을 편집하여 실패한 노드 코드를 포함 할 수 있습니까? – JohnnyHK
이 두 컬렉션의 샘플 데이터가 있습니까? 배열의 요소를 제거하려는 경우 [$ pull] (http://docs.mongodb.org/manual/reference/operator/update/pull/) 연산자를 살펴 봐야합니다. – yaoxing